gpt4 book ai didi

javascript - ajax 调用后,curl 和 file_get_contents 不起作用

转载 作者:行者123 更新时间:2023-11-28 10:48:51 35 4
gpt4 key购买 nike

我正在尝试使用坐标检索用户的城市信息。有一个 javascript 函数可以获取纬度/经度并使用 ajax 将其发送到 php 文件:

Javascript函数

if (navigator.geolocation) { //Checks if browser supports geolocation
var Clatitude = "no";
var Clongitude;

navigator.geolocation.getCurrentPosition(function(position) {
Clatitude = position.coords.latitude;
Clongitude = position.coords.longitude;
$.ajax({
type: "POST",
url: "../wp-content/themes/Avada-child/test_BLZ.php",
data: {'latitudine': Clatitude,'longitudine': Clongitude},
success: function(data) {
console.log(data);
},
dataType: "JSON"
});
},
function(error) {

alert(error.message);
}, {
enableHighAccuracy: true,
timeout: 5000
});
}

用 PHP 编写的服务器端脚本

<?php

$latitudine = $_POST['latitudine'];
$longitudine = $_POST['longitudine'];
$geolocation = $latitudine.','.$longitudine;
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false';
$file_contents = url_get_contents($request);
echo ($file_contents);

function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

问题是响应没有显示在我的控制台中。我尝试使用 file_get_contents 获取响应,但仍然无法检索响应

除此之外,其余代码都可以正常工作。事实上,一旦我从 php 文件中删除 curl's 内容或 file_get_contents,其余代码就可以顺利运行。我什至设法在 ajax 成功函数中检索到(假)响应。

有人可以帮忙吗?谢谢;)

---编辑我的curl执行时间很长,我在另一台服务器上运行相同的代码,这些是差异:

curl 执行时间较长的服务器

{
   "url": "http://maps.googleapis.com/maps/api/geocode/json?latlng=45.644843,8.9986268&sensor=false",
   "content_type": "application/json; charset=UTF-8",
   "http_code": 200,
   "header_size": 377,
   "request_size": 119,
   "filetime": -1,
   "ssl_verify_result": 0,
   "redirect_count": 0,
   "total_time": 127.26954,
   "namelookup_time": 0.001964,
   "connect_time": 127.23926,
   "pretransfer_time": 127.239265,
   "size_upload": 0,
   "size_download": 11729,
   "speed_download": 92,
   "speed_upload": 0,
   "download_content_length": -1,
   "upload_content_length": 0,
   "starttransfer_time": 127.269424,
   "redirect_time": 0,
   "certinfo": []
"request_header":"GET \/maps\/api\/geocode\/json?latlng=45.644843,8.9986268&sensor=false HTTP\/1.1\r\nHost: maps.googleapis.com\r\nAccept: *\/*\r\n\r\n"
}

curl执行时间短的服务器

{
   "url": "http://maps.googleapis.com/maps/api/geocode/json?latlng=45.644843,8.9986268&sensor=false",
   "content_type": "application/json; charset=UTF-8",
   "http_code": 200,
   "header_size": 377,
   "request_size": 119,
   "filetime": -1,
   "ssl_verify_result": 0,
   "redirect_count": 0,
   "total_time": 0.116182,
   "namelookup_time": 0.012194,
   "connect_time": 0.027452,
   "pretransfer_time": 0.027473,
   "size_upload": 0,
   "size_download": 11729,
   "speed_download": 100953,
   "speed_upload": 0,
   "download_content_length": -1,
   "upload_content_length": 0,
   "starttransfer_time": 0.114101,
   "redirect_time": 0,
   "redirect_url": "",
   "primary_ip": "172.217.**.**",
   "certinfo": [],
   "primary_port": 80,
   "local_ip": "192.168.**.**",
   "local_port": 51393
"request_header":"GET \/maps\/api\/geocode\/json?latlng=45.644843,8.9986268&sensor=false HTTP\/1.1\r\nHost: maps.googleapis.com\r\nAccept: *\/*\r\n\r\n"
}

我的服务器出了什么问题?我注意到在第一种情况下没有 ip 引用!

最佳答案

由于服务器的响应不是采用 JSON 形式,因此您不应在 AJAX 调用中设置 dataType:"JSON" .

请从 AJAX 调用中将其注释掉。

$.ajax({
type: "POST",
url: "../wp-content/themes/Avada-child/test_BLZ.php",
data: {'latitudine': Clatitude,'longitudine': Clongitude},
success: function(data) {
console.log(data);
},
//dataType: "JSON" <-- COMMENT this out
});



另一种选择是您应该从服务器发送 JSON 编码响应。

$file_contents = url_get_contents($request);
echo (json_encode($file_contents));

关于javascript - ajax 调用后,curl 和 file_get_contents 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37272655/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com