Javascript Chrome 中的“Access-Control-Allow-Origin 不允许使用 Origin null”。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5224017/
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 16:14:05  来源:igfitidea点击:

"Origin null is not allowed by Access-Control-Allow-Origin" in Chrome. Why?

javascriptgoogle-chromexmlhttprequestcross-domain

提问by Moss

I am working on some Javascript to run locally on my PC. I am using a jQuery CSV plugin (http://plugins.jquery.com/project/csv) to load load a csv file into javascript arrays. The script is simple:

我正在研究一些 Javascript 以在我的 PC 上本地运行。我正在使用 jQuery CSV 插件 (http://plugins.jquery.com/project/csv) 将 csv 文件加载到 javascript 数组中。脚本很简单:

$(function(){
 $.get("file.csv", function(data){
  stuff = $.csv()(data);
 })
})

In Firefox it works fine but in Chrome it says "Origin null is not allowed by Access-Control-Allow-Origin". What does that mean? I find all sorts of threads about cross-server stuff related to this error but I am just working with local files.

在 Firefox 中它工作正常,但在 Chrome 中它说“Access-Control-Allow-Origin 不允许 Origin null”。这意味着什么?我找到了与此错误相关的跨服务器内容的各种线程,但我只是在处理本地文件。

回答by Pointy

Chrome doesn't believe that there's any common relationship between any two local files.

Chrome 不相信任何两个本地文件之间存在任何共同关系。

You can start it with the option "--allow-file-access-from-files" to tell it you disagree.

您可以使用“--allow-file-access-from-files”选项启动它,告诉它您不同意。

Thanks to the ascendant master Nick Craver for this info when I asked essentially the same question some time ago.

感谢上升大师 Nick Craver 在我前段时间问基本相同的问题时提供的信息。

回答by WebSeed

If you are using Mac OS X, open up the Terminal, cdto your web root and run:

如果您使用的是 Mac OS X,请打开终端cd到您的 Web 根目录并运行:

python -m SimpleHTTPServer

Then open the following URL in Chrome (or any other web browser):

然后在 Chrome(或任何其他网络浏览器)中打开以下 URL:

http://0.0.0.0:8000

回答by Cu7l4ss

It's like crossdomain for some unclear reason (each browser acts a bit differently regarding this issue, you could even try IE and see the results). You should try and run it through a web server, and give it an absolute path since the javascript runs locally.

这就像出于某种不明原因的跨域(每个浏览器在这个问题上的行为略有不同,您甚至可以尝试 IE 并查看结果)。您应该尝试通过 Web 服务器运行它,并给它一个绝对路径,因为 javascript 在本地运行。

回答by Madhukarah

You can try running it on your apache web server. It will work.

您可以尝试在您的 apache 网络服务器上运行它。它会起作用。