我在这一行收到错误:
var myObj = JSON.parse(url.responseText);
我无法弄明白为什么。有人知道吗?用户键入JSON文件所在的URL。
function loadJson() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var myObj = JSON.parse(url.responseText);
var list = "<table border = '1'><tr><th>Name</th><th>Address</th><th>Major</th><th>GPA</th></tr>\n";
for (var i = 0; i < myObj.students.length; i++)
{
list = list + "<tr><td>" + myObj.students[i].first + " " + myObj.students[i].last + "</td>" +
"<td>" + myObj.students[i].address.city + ", " + myObj.students[i].address.state + " " +
myObj.students[i].address.zip + "</td>" +
"<td>" + myObj.students[i].major + "</td>" +
"<td>" + myObj.students[i].gpa + "</td></tr>\n";
}
document.getElementById("json").innerHTML = list;
}
}
var url=document.getElementById("url").value;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
答案 0 :(得分:1)
来自ajax请求的响应文本是XMLHttpRequest
对象
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
var myObj = JSON.parse(this.responseText);
...
}
}