[英]AJAX XMLHttpRequest error
I have this basic "GET" request on a demo page I'm building via localhost. 我在通过localhost构建的演示页面上有这个基本的“ GET”请求。 Both json and html file are in the same folder.
json和html文件都在同一文件夹中。
http://localhost.com/test.html
http://localhost.com/myList.json
Unfortunately when making the request I get this error: 不幸的是,在发出请求时出现此错误:
Uncaught ReferenceError: results is not defined.
Uncaught TypeError: Cannot set property 'innerHTML' of null
My html is as follows. 我的HTML如下。
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function get_json_request() {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "myList.json", true);
httpRequest.setRequestHeader("Content-type", "application/json", true);
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
var results = document.getElementById("results");
results.innerHTML = data.user;
} else {
alert('There was a problem with the request.');
}
}
httpRequest.send(null);
results.innerHTML = "Processing....";
}
</script>
</head>
<body>
<div id"results"></div>
<script type="text/javascript">get_json_request();</script>
</body>
</html>
Any help appreciated. 任何帮助表示赞赏。
You're setting the reference to "results" in a conditional statement. 您在条件语句中设置对“结果”的引用。 If that variable is not initialised, it will naturally return null.
如果未初始化该变量,则它自然会返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.