[英]Json object returned from PHP with json_encode() , but then invalid json object in my javascript
[英]JSON Object returned from PHP is NULL
我正在嘗試使用AJAX將JavaScript變量(占位符)發送到PHP。 我使用此變量來檢索JSON對象。 返回給Javascript的JSON為NULL。 我該如何解決?
function sendToPhp(placeid){
var url = "finals.php?placeid="+placeid;
var getJSONObj=function(url,callback){
var httpc = new XMLHttpRequest();
httpc.open("GET", url, true);
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpc.responseType='json';
httpc.onload= function(){
var status=httpc.status;
if(status==200){
//alert(httpc.response);
callback(null,httpc.response);
}
else{
callback(status,httpc.response);
}
};
httpc.send();
};
getJSONObj(url,function(err,jsonObjectReturned){
if(err!==null){
alert("something went wrong"+ err);
}
else
{
alert("success");
alert(jsonObjectReturned); // **returns NULL**
}
});
} // end of function
PHP腳本使用Placeid返回一個JSON文件,如下所示:
if(isset($_GET['placeid']))
{
$placeid= $_GET['placeid'];
$apikey="someKeyValue";
$url="https://maps.googleapis.com/maps/api/place/details/json?placeid=".$placeid."&key=".$apikey;
$jsonPlacesObject=json_decode(htmlspecialchars(@file_get_contents($url),true),true);
echo json_encode($jsonPlacesObject); //sending json to javascript**
die();
}
您的AJAX表單看起來有誤。 嘗試閱讀https://www.w3schools.com/js/js_json_php.asp 。
原版的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.