我正在使用AJAX
调用来请求服务器上的PHP
文件。 PHP
文件包含一些对象。正在使用JSON.parse()
将结果转换为JavaScript
对象。所以,问题是运行程序我看到下面的错误在浏览器控制台后:从服务器上的PHP文件获取数据作为JSON
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at XMLHttpRequest.xmlhttp.onreadystatechange (index.php:15)
我不知道为什么程序不能正常工作,虽然我的Apache服务器,和PHP运行良好。 我的工作程序:
的index.php:
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "demo_file.php", true);
xmlhttp.send();
</script>
demo_file.php:
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
非常感谢。我想要抱你! –