When trying to do a HTTP request using XMLHttpRequest from a local file, it basically fails due to Access-Control-Allow-Origin
violation.
当尝试使用来自本地文件的XMLHttpRequest执行HTTP请求时,由于访问控制允许的违反,它基本上失败了。
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.
但是,我自己使用本地的web页面,所以我想知道是否有任何方法可以让谷歌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?
那么,如何允许谷歌Chrome使用XMLHttpRequest从本地文件加载url呢?
4 个解决方案
#1
43
startup chrome with --disable-web-security
与——disable-web-security启动chrome
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 disables the web security, so be careful with it.
而且,这正是您所期望的,它禁用了web安全性,所以要小心。
#2
197
Using --disable-web-security
switch 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-files
switch?
使用——禁用web安全切换非常危险!为什么要禁用安全性,而只允许XMLHttpRequest使用allow-file-access-from-files开关从其他文件访问文件?
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这一“特性”的讨论:
- https://bugs.chromium.org/p/chromium/issues/detail?id=37586,
- https://bugs.chromium.org/p/chromium/issues/detail?id=37586,
- https://bugs.chromium.org/p/chromium/issues/detail?id=47416,
- https://bugs.chromium.org/p/chromium/issues/detail?id=47416,
- https://productforums.google.com/forum/#!topic/chrome/v177zA6LCKU
- https://productforums.google.com/forum/ # ! /铬/ v177zA6LCKU话题
#3
20
Mac version. From terminal run:
Mac版本。从终端运行:
open /Applications/Google\ Chrome.app/ --args --allow-file-access-from-files
#4
3
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/
引用
#1
43
startup chrome with --disable-web-security
与——disable-web-security启动chrome
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 disables the web security, so be careful with it.
而且,这正是您所期望的,它禁用了web安全性,所以要小心。
#2
197
Using --disable-web-security
switch 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-files
switch?
使用——禁用web安全切换非常危险!为什么要禁用安全性,而只允许XMLHttpRequest使用allow-file-access-from-files开关从其他文件访问文件?
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这一“特性”的讨论:
- https://bugs.chromium.org/p/chromium/issues/detail?id=37586,
- https://bugs.chromium.org/p/chromium/issues/detail?id=37586,
- https://bugs.chromium.org/p/chromium/issues/detail?id=47416,
- https://bugs.chromium.org/p/chromium/issues/detail?id=47416,
- https://productforums.google.com/forum/#!topic/chrome/v177zA6LCKU
- https://productforums.google.com/forum/ # ! /铬/ v177zA6LCKU话题
#3
20
Mac version. From terminal run:
Mac版本。从终端运行:
open /Applications/Google\ Chrome.app/ --args --allow-file-access-from-files
#4
3
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/
引用