Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.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在chrome中获取文件_Javascript_Ajax_Google Chrome - Fatal编程技术网

Javascript 无法通过AJAX在chrome中获取文件

Javascript 无法通过AJAX在chrome中获取文件,javascript,ajax,google-chrome,Javascript,Ajax,Google Chrome,我正在用vanilla js学习AJAX。 下面的代码使我获得了一个get请求Ajax document.addEventListener('DOMContentLoaded', () => { console.log('ready!'); let xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (xhr.readyState === 4) document.getElementB

我正在用vanilla js学习AJAX。 下面的代码使我获得了一个get请求Ajax

document.addEventListener('DOMContentLoaded', () => {
console.log('ready!');
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
    if (xhr.readyState === 4)
        document.getElementById('file-data').innerText = xhr.responseText;
    xhr.send();
}
xhr.open('GET', 'file:///E:ada/kk.js', true);
});
这在Firefox中非常有效,但在chrome中,我遇到以下错误:-

kk.js:7 Failed to load file:///E:/ada/kk.js: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
xhr.onreadystatechange@kk.js:7

kk.js:7 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.
at XMLHttpRequest.xhr.onreadystatechange (file:///E:/ada/kk.js:7:9)
at XMLHttpRequest.xhr.onreadystatechange (file:///E:/ada/kk.js:7:9)
at HTMLDocument.document.addEventListener (file:///E:/ada/kk.js:9:5)

这是因为您正在从本地计算机加载文件,并且由于它不是http调用,因此会收到cors错误消息

您应该将文件托管在url中的某个位置,如 http://example.com/path/to/file 或者在本地服务器上提供服务