Javascript AJAX XMLHttpRequest错误

Javascript AJAX XMLHttpRequest错误,javascript,html,ajax,Javascript,Html,Ajax,我在通过localhost构建的演示页面上有一个基本的“GET”请求。 json和html文件都在同一个文件夹中 http://localhost.com/test.html http://localhost.com/myList.json 不幸的是,在发出请求时,我遇到以下错误: Uncaught ReferenceError: results is not defined. Uncaught TypeError: Cannot set property 'innerHTML' of nu

我在通过localhost构建的演示页面上有一个基本的“GET”请求。 json和html文件都在同一个文件夹中

http://localhost.com/test.html
http://localhost.com/myList.json
不幸的是,在发出请求时,我遇到以下错误:

Uncaught ReferenceError: results is not defined.

Uncaught TypeError: Cannot set property 'innerHTML' of null 
我的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>

试验
函数get_json_request(){
var-httpRequest;
httpRequest=新的XMLHttpRequest();
open(“GET”,“myList.json”,true);
setRequestHeader(“内容类型”,“应用程序/json”,true);
httpRequest.onreadystatechange=函数(){
if(httpRequest.readyState==4&&httpRequest.status==200){
var data=JSON.parse(httpRequest.responseText);
var results=document.getElementById(“结果”);
results.innerHTML=data.user;
}否则{
警报(“请求有问题”);
}
}
httpRequest.send(空);
results.innerHTML=“处理…”;
}
获取json请求();

感谢您的帮助。

您正在条件语句中设置对“结果”的引用。如果该变量未初始化,它将自然返回null。

您正在条件语句中设置对“results”的引用。如果该变量未初始化,它将自然返回null。

打字
可能重复的打字
可能重复的打字
这是潜在的第二个问题。第一个问题是Ajax是异步的。请参阅前面的投票,以重复结束,并对问题本身进行相关评论。这是潜在的第二个问题。第一个问题是Ajax是异步的。请参阅前面的投票,以重复方式结束,并对问题本身进行相关评论。