Javascript JS没有解析PHP ResponseText
我有一个JS-PHP脚本,JS将信息发送到PHP脚本,然后在我的HTML页面中显示PHP响应。如果在一个区域中显示responseText,脚本将正常工作。当我试图解析responseText以便在不同区域显示它时,脚本无法工作。我哪里做错了 我的PHP脚本结束:Javascript JS没有解析PHP ResponseText,javascript,php,json,responsetext,Javascript,Php,Json,Responsetext,我有一个JS-PHP脚本,JS将信息发送到PHP脚本,然后在我的HTML页面中显示PHP响应。如果在一个区域中显示responseText,脚本将正常工作。当我试图解析responseText以便在不同区域显示它时,脚本无法工作。我哪里做错了 我的PHP脚本结束: ... $expert = $obj; $pro = $obj1; $superview = $obj2; echo json_encode(array(first=>$expert,second=>$pro,third=
...
$expert = $obj;
$pro = $obj1;
$superview = $obj2;
echo json_encode(array(first=>$expert,second=>$pro,third=>$superview));
?>
PHP响应:
{"first":1860,"second":1100,"third":340}
JavaScript:
// this calls the php script with the data we have collected from the geolocation lookup
function GEOajax(url) {
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
// this reads the response from the php script and updates the page with it's output
function updatePage() {
if (xmlHttp.readyState == 4 && xmlhttp.status==200) {
var response = JSON.parse(xmlhttp.responseText);
document.getElementById("geo").innerHTML='' + response.first;
document.getElementById("geo1").innerHTML='' + response.second;
document.getElementById("geo2").innerHTML='' + response.third;
}
}
HTML:
如何解析PHP响应,以便在HTML的不同区域显示不同的变量?您的XHR变量在某些地方称为
xmlHttp
,在其他地方称为xmlHttp
。您是否检查了开发人员控制台的错误?正确!感谢您的帮助-我多次查看代码,但始终没有看到!
<div id="geo" ></div>
<div id="geo1" ></div>
<div id="geo2" ></div>
// this reads the response from the php script and updates the page with it's output
function updatePage() {
if (xmlHttp.readyState == 4) {
var response1 = xmlHttp.responseText;
document.getElementById("geo").innerHTML = '' + response1;
}
}