【发布时间】:2017-06-17 22:27:53
【问题描述】:
我正在使用AJAX 调用从服务器请求PHP 文件。 PHP 文件包含一些对象。 JSON.parse() 用于将结果转换为 JavaScript 对象。所以问题是运行程序后我在浏览器控制台中看到以下错误:
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";
【问题讨论】:
标签: javascript php json ajax xmlhttprequest