Javascript Ajax请求包含JSON-readystate 4,状态0
我正在对我的网站中包含的JSON文件执行Ajax GET请求。因此,JSON文件包含在单独的页面中。请求的readystate为“4”,而状态为“0”。因此,请求已发送,但responsetext为空 我已经验证了JSON数据。 javascript文件返回HTML5页面上应该返回的数据。 错误处理为readystate“4”和状态“0” 这是javascript文件的代码,JSON数据正确且经过测试。。HTML5连接也可以正常工作,经过测试。。javascript文件在HTML中的正确元素上插入数据 有人能发现错误吗?谢谢大家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中的正确元素上插
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
的顶部。让我知道它是否有效
因为
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"}
]
}