我试图从名为questions.json的JSON文件中检索数组(与JavaScript文件位于同一文件夹中)。为了克服相同的原始策略,我允许XMLHttpRequest使用Chrome --allow-file-access-from-files
开关访问本地文件。
我已经查询了与此相关的其他问题,但我仍然不确定我的请求失败的原因。失败是否与我对本地主机的AJAX调用有关?我正在做一个有效的HTTP请求?我没有克服同样的原产地政策吗?
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
console.log(httpRequest.status);
if (httpRequest.status === 200) {
var allQuestions = httpRequest.responseText
} else {
console.log('There was a problem with the request.')
}
} else {
console.log('The request is still not ready.')
}
}
httpRequest.open('GET', 'questions.json', true);
httpRequest.send(null);
我的控制台显示请求仍未准备好(3次),XHR已完成加载,httpRequest.status
为0,然后请求出现问题。