Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax请求包含JSON-readystate 4,状态0_Javascript_Html_Ajax - Fatal编程技术网

Javascript Ajax请求包含JSON-readystate 4,状态0

Javascript Ajax请求包含JSON-readystate 4,状态0,javascript,html,ajax,Javascript,Html,Ajax,我正在对我的网站中包含的JSON文件执行Ajax GET请求。因此,JSON文件包含在单独的页面中。请求的readystate为“4”,而状态为“0”。因此,请求已发送,但responsetext为空 我已经验证了JSON数据。 javascript文件返回HTML5页面上应该返回的数据。 错误处理为readystate“4”和状态“0” 这是javascript文件的代码,JSON数据正确且经过测试。。HTML5连接也可以正常工作,经过测试。。javascript文件在HTML中的正确元素上插

我正在对我的网站中包含的JSON文件执行Ajax GET请求。因此,JSON文件包含在单独的页面中。请求的readystate为“4”,而状态为“0”。因此,请求已发送,但responsetext为空

我已经验证了JSON数据。 javascript文件返回HTML5页面上应该返回的数据。 错误处理为readystate“4”和状态“0”

这是javascript文件的代码,JSON数据正确且经过测试。。HTML5连接也可以正常工作,经过测试。。javascript文件在HTML中的正确元素上插入数据

有人能发现错误吗?谢谢大家

var xhr = new XMLHttpRequest();                 //create Request Object

xhr.onload = function(){                        //when the Request is loaded, parse it in a responsetext 

if(xhr.status === 200){

    var responseObject = JSON.parse(xhr.responseText);

    var newContent ='';

    for (var i=0; i<responseObject.events.length; i++){

        newContent += responseObject.events[i].color;

        newContent += responseObject.events[i].value;

    }

    document.getElementById("jsonreturn_option").innerHTML = newContent;
}

else{

    document.getElementById("jsonreturn_option").innerHTML = xhr.readyState + "    " + xhr.status  ;

    //readystate is 4, which means that the request has been sent.. 
    //request status is 0, which means that the responsetext is empty.. 
}
};

xhr.open('GET','json/options_form.json',true);
xhr.send(null);                                         //no additional data to be sent

尝试
xhr.onreadystatechange
而不是
xhr.onload
,并将
xhr.open
xhr.send
移动到
xhr.onload
的顶部。让我知道它是否有效

  • 您需要创建javaweb项目,然后设置tomcat服务器

  • 将所有文件放入javaweb项目,然后启动tomcat服务器

  • 打开浏览器类型,如:


  • 因为
    xhr.open('GET','json/options_form.json',true)无法请求本地文件,必须请求服务器文件

    谢谢您的回复!我仍然有相同的日志,所以readystate 4和状态0的问题,空响应文本..谢谢verejava,这对我帮助很大!我进一步查看了web,发现有另一种方法可以做到这一点,即使用FileReader()。此对象允许应用程序读取本地存储的文件。
    {
    "events":[
    
    {  "color": "red", "value": "#f00"},
    {  "color": "green", "value": "#0f0"},
    {  "color": "blue", "value": "#00f"},
    {  "color": "cyan", "value": "#0ff"}
    ]
    }