有没有办法绕过 Javascript / jQuery 的本地访问同源策略?
尝试使用 ajax
、getJSON
和类似函数从本地(非服务器)开发计算机获取外部 URL。有没有办法绕过同源策略,以便我可以在本地测试,而不必上传到服务器?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
尝试使用 ajax
、getJSON
和类似函数从本地(非服务器)开发计算机获取外部 URL。有没有办法绕过同源策略,以便我可以在本地测试,而不必上传到服务器?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
这是简单的答案: chrome --disable-web-security
从源代码(chrome_switches.h):
我想使用 jquery.js 将 AJAX 调用发送到在端口 8080 上运行的 Google Apps python 服务器。只是为了测试,我想要在同一台机器上运行浏览器和服务器。
我不明白所有的安全细微差别,但对于临时开发来说,这似乎是一个合理的解决方法。只要我只使用 chrome 来测试这个标志,就不会有问题。
以下是 Mac OS X 的完整命令:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security
Here's the simple answer: chrome --disable-web-security
From the source code (chrome_switches.h):
I wanted to use jquery.js to send AJAX calls to a Google Apps python server running on port 8080. Just for testing, I wanted to run the browser and the server on the same machine.
I don't understand all the security nuances, but for temporary development it seems like a reasonable workaround. So long as I only use chrome for testing with this flag, it shouldn't be a problem.
Here's the whole command for Mac OS X:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security
我们在开发网络应用程序时也有同样的需求。我们是这样做的:
浏览器和服务器仅通过 JSON 进行通信。
所有 HTML 均使用 PURE(我们的 JS 模板引擎)在浏览器中呈现。
浏览器代码是这样在本地开发的:
我们在应用程序的 url 中添加一个
host
参数:在生产中,JSON 通过 POST 发送到服务器。
但这里负责 ajax 调用的函数将对
host
参数做出反应,并生成一个 JSONP 注入(GET)。f2309892
是一个临时函数,具有随机名称,它指向处理响应的方法json
是我们发送到服务器的 JSON这意味着您将需要一些后端的合作为您提供封装在回调函数中的 json,例如:
除了大小限制(您不能使用 GET 向服务器发送大 JSON)之外,它的工作方式轻而易举。
另一个优点是您可以从同一个本地主机调用所有不同的系统(开发和测试)。
We had the same need when developing our web app. Here's how we did it:
The browser and the server communicate only through JSON.
All the HTML is rendered in the browser using PURE (our JS template engine).
The browser code is developed locally like this:
We add a
host
parameter in the url of the app:In production, the JSON are sent to the server with a POST.
But here the function in charge of the ajax call will react to the
host
parameter and make a JSONP injection(GET) instead.f2309892
is a temporary function, with a random name, that points to the method that will handle the responsejson
is the JSON we send to the serverIt means you will need some cooperation from the backend to serve you the json wrapped in a callback function like:
Except a size limitation(you can't send a big JSON to the server with a GET) it works like a breeze.
An other advantage is you can call all the different systems(development and test) from the same localhost.
有不同的方法可以解决这个问题,具体取决于您用于开发的浏览器。例如:
security.fileuri.strict_origin_policy
设置为false
--allow-file-access 启动浏览器-from-files
参考:Firefox、Chrome
There are different ways to get around this, depending on which browser you're using for development. For example:
security.fileuri.strict_origin_policy
tofalse
--allow-file-access-from-files
References: Firefox, Chrome
无需接触服务器 -
在 Firefox 中绕过同源安全策略的最快、最简单的方法是安装 Force CORS 插件。通过将正确的标头插入到每个响应中,这适用于任何服务。
https://addons.mozilla.org/en-US/firefox/addon/力量/
Without touching the server -
The quickest and easiest way to bypass the same origin security policy in Firefox is the install the Force CORS add-on. This works with any service by inserting the proper headers into every response.
https://addons.mozilla.org/en-US/firefox/addon/forcecors/
由于这是一个开发问题而不是最终用户/功能问题,因此不要专注于让 AJAX 跨域,而是将您的开发环境设置为代理以从生产服务器获取最新数据。这实际上很容易做到。
您需要在开发环境中设置一台 Web 服务器(如果还没有),然后将服务器配置为通过获取并回显生产数据来响应 404 请求。您可以设置您的服务器,以便仅拾取 AJAX 数据文件(否则,如果生产资产开始显示在您的开发页面上,调试其他文件将会很混乱)。因此,如果
http://dev.myserver.com/data/json/mydata.json
丢失,您的 404 脚本将得到http://prod.myserver.com/data/json /mydata.json
并将其回显给客户端。这种设置的好处是您可以非常轻松地使用模拟数据:如果您的开发环境中存在该文件,您的 AJAX 脚本就会获取该文件;但如果您随后删除或重命名该文件,您将获得生产数据。这个功能非常有用,我强烈推荐它。如果您使用 XML,我建议您复制 404 中的 HTTP 标头。如果您的 404 进程响应
Content-Type
为text/html
,那么您不会获得任何要解析的responseXML
。Since this is a development issue and not a end-user/functionality issue, rather than focusing on getting AJAX to cross domains get your development environment set up as a proxy to fetch the most recent data from the production servers. This is actually really easy to do.
You'd need to set up a web server in your dev environment (if it doesn't have one already), and then configure the server to respond to 404 requests by fetching and then echoing production data. You can set up your server so that only the AJAX data files are picked up (otherwise, it will be confusing to debug other files if production assets start showing up on your development pages). So if
http://dev.myserver.com/data/json/mydata.json
is missing, your 404 script will gethttp://prod.myserver.com/data/json/mydata.json
and echo it to the client. The nice thing about this set-up is that you can use mock data very easily: if the file is there in your dev environment, your AJAX script will get that; but if you then erase or rename that file, you'll get the production data instead. This feature has been so useful I can't recommend it enough.If you're working with XML, I'd recommend duplicating the HTTP headers in the 404. If your 404 process responds with a
Content-Type
oftext/html
, you won't get anyresponseXML
to parse.试试这个(php curl ayax跨域 - 谷歌):
http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/
http://www.phpfour.com/blog/2008/03 /cross-domain-ajax-using-php/
http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html
try this (php curl ayax cross domain - by google):
http://www.iacons.net/writing/2007/08/02/ajax-cross-domain-proxy/
http://www.phpfour.com/blog/2008/03/cross-domain-ajax-using-php/
http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html
我也遇到了这个问题,使用 Chrome 并且
--allow-file-access-from-files
选项并没有真正帮助。回到我的服务器需要返回的脚本,我将这些标头添加到响应中并且工作正常:另一个标头用于允许某种密钥交换
I had that problem, too, using Chrome and the
--allow-file-access-from-files
option didn't really help. Back to the script my server needed to return, I added these headers to the response and it worked fine :and another one for allowing a sort of key exchange
不允许在 CORS 中使用 localhost http://code.google.com /p/chromium/issues/detail?id=67743 使用 lvh.me 代替
localhost is not allowed to use in CORS http://code.google.com/p/chromium/issues/detail?id=67743 use lvh.me instead