Javascript CORS 策略已阻止来自源“null”的请求:跨源请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55252192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:16:08  来源:igfitidea点击:

from origin 'null' has been blocked by CORS policy: Cross origin requests

javascriptjqueryjsongetjson

提问by index

This error occurred while calling up JSON. I don't know why this error is happening.

调用 JSON 时发生此错误。我不知道为什么会发生这个错误。

$.getJSON(url, function(data){
    console.log(data);
});

Error:

错误:

Access to XMLHttpRequest at (this is JSON URL) from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

回答by Ezekiel Victor

The error message says it all. You are apparently accessing the page running this script with a protocol scheme other than the several supported for cross-origin requests. This would happen commonly if you are, say, executing a local file in your browser via the file:// protocol and trying to access some URL on the Internet in your $.getJSONcall.

错误消息说明了一切。您显然正在使用除跨域请求支持的几种协议方案之外的协议方案访问运行此脚本的页面。例如,如果您通过 file:// 协议在浏览器中执行本地文件并尝试在$.getJSON通话中访问 Internet 上的某个 URL,则通常会发生这种情况。