简体   繁体   English

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

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

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请求时,由于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。

Eg, $.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加载网址?

Using --disable-web-security switch is quite dangerous ! 使用--disable-web-security开关非常危险 Why disable security at all while you can just allow XMLHttpRequest to access files from other files using --allow-file-access-from-files switch? 为什么要禁用安全性,同时允许XMLHttpRequest使用--allow-file-access-from-files开关从其他文件--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的这个“功能”

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安全性,因此请小心。

Mac version. Mac版。 From terminal run: 从终端运行:

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

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 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM