无法通过AJAX在Chrome中获取文件

时间:2017-12-16 14:50:59

标签: javascript ajax google-chrome

我在vanilla js学习AJAX。 我通过以下代码获得了get请求(AJAX)。

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.

这在Firefox中完美运行但在chrome中我遇到以下错误: -

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)

xhr.onreadystatechange @ kk.js:7

     {
        "title" : "api recipe",
        "directions" : "Testing api recipe directions",
        "ingredient1" : "postman",
        "ingredient2" : "ing2",
        "ingredient3" : "me",
        "ingredient4" : "ingredient4",
        "ingredient5" : "ingredient5"
     }

1 个答案:

答案 0 :(得分:1)

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

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