Javascript 允许 Google Chrome 使用 XMLHttpRequest 从本地文件加载 URL

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

Allow Google Chrome to use XMLHttpRequest to load a URL from a local file

javascriptjquerygoogle-chromexmlhttprequestlocal-files

提问by pimvdb

When trying to do a HTTP request using XMLHttpRequest from a local file, it basically fails due to Access-Control-Allow-Originviolation.

当尝试使用来自本地文件的 XMLHttpRequest 执行 HTTP 请求时,它基本上由于Access-Control-Allow-Origin违规而失败。

However, I'm using the local web page myself, so I was wondering if there is any way to make Google Chrome allow these requests, which are from a local file to a URL on the Internet.

但是,我自己使用的是本地网页,所以我想知道是否有任何方法可以让 Google Chrome 允许这些请求,这些请求是从本地文件到 Internet 上的 URL。

E.g., $.get('http://www.google.com/')fails when executing in a local file, but I've scripted the page myself and I'm using it myself, so it would be extremely useful if I could suppress it and load the URL.

例如,$.get('http://www.google.com/')在本地文件中执行时失败,但我自己编写了页面脚本并且我自己正在使用它,所以如果我可以抑制它并加载 URL,这将非常有用。

So, how can I allow Google Chrome to load URLs using XMLHttpRequest from local files?

那么,如何允许 Google Chrome 从本地文件使用 XMLHttpRequest 加载 URL?

采纳答案by jAndy

startup chrome with --disable-web-security

启动镀铬 --disable-web-security

On Windows:

在 Windows 上:

chrome.exe --disable-web-security

On Mac:

在 Mac 上:

open /Applications/Google\ Chrome.app/ --args --disable-web-security

This will allow for cross-domain requests.
I'm not aware of if this also works for local files, but let us know !

这将允许跨域请求。
我不知道这是否也适用于本地文件,但请告诉我们!

And mention, this does exactly what you expect, it disablesthe web security, so be careful with it.

并提到,这正是您所期望的,它禁用了网络安全,所以要小心。

回答by Konstantin Smolyanin

Using --disable-web-securityswitch is quite dangerous! Why disable security at all while you can just allow XMLHttpRequest to access files from other files using --allow-file-access-from-filesswitch?

使用--disable-web-securityswitch 是相当危险的!为什么要完全禁用安全性,而您可以只允许 XMLHttpRequest 使用--allow-file-access-from-filesswitch从其他文件访问文件?

Before using these commands be sure to end all running instances of Chrome.

在使用这些命令之前,请务必结束所有正在运行的 Chrome 实例。

On Windows:

在 Windows 上:

chrome.exe --allow-file-access-from-files

On Mac:

在 Mac 上:

open /Applications/Google\ Chrome.app/ --args --allow-file-access-from-files

Discussions of this "feature"of Chrome:

关于Chrome的这个“特性”的讨论:

回答by Nek

Mac version. From terminal run:

Mac版。从终端运行:

open /Applications/Google\ Chrome.app/ --args --allow-file-access-from-files

回答by Taimoor Changaiz

On Ubuntu:

在 Ubuntu 上:

chromium-browser --disable-web-security

For more details/switches:

有关更多详细信息/开关:

http://peter.sh/experiments/chromium-command-line-switches/

http://peter.sh/experiments/chromium-command-line-switches/

Referenced from

引用自