有什么方法可以通过 Access-Control-Allow-Origin 在我自己的服务器上进行开发吗?
我收到此 JavaScript 错误
XMLHttpRequest 无法加载 http://foo.bar.no/API/map_tools/clean_addresses/check。 Access-Control-Allow-Origin 不允许来源 http://foo.bar.no:9294。
这些都位于同一域和同一服务器上,但我的 JavaScript 项目由一个独立的服务器脚本托管,该脚本会自动将 JavaScript 及其依赖项捆绑到一个文件中。
在开发过程中如何克服这个限制?
我尝试过允许我的 JavaScript 服务器脚本进行连接。这是对 url 进行卷曲的结果:
HTTP/1.1 200 OK Date: Wed, 11 Jan 2012 09:05:14 GMT Server: Apache/2.2.16 (Debian) Access-Control-Allow-Origin: http://foo.bar.no:9294 Vary: Accept-Encoding Content-Length: 70 Content-Type: text/plain array(1) { ["q"]=> string(31) "map_tools/clean_addresses/check" }
但我仍然得到与上面粘贴的完全相同的错误。为什么 Chrome 明明允许连接到该死的 URL 却仍然拒绝连接!?
I'm getting this JavaScript error
XMLHttpRequest cannot load http://foo.bar.no/API/map_tools/clean_addresses/check. Origin http://foo.bar.no:9294 is not allowed by Access-Control-Allow-Origin.
This is all on the same domain and the same server, but my JavaScript project is being hosted by a standalone server script that automatically bundles the JavaScript and it's dependencies into one file.
How do I get past this restriction while I'm developing?
I've tried allowing my JavaScript server script to connect. This is the result of a curl to the url:
HTTP/1.1 200 OK Date: Wed, 11 Jan 2012 09:05:14 GMT Server: Apache/2.2.16 (Debian) Access-Control-Allow-Origin: http://foo.bar.no:9294 Vary: Accept-Encoding Content-Length: 70 Content-Type: text/plain array(1) { ["q"]=> string(31) "map_tools/clean_addresses/check" }
And still I get the exact same error as I pasted above. Why does Chrome still refuse to connect to the damn URL when it's obviously allowed to!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想通了。我一直在寻找一种简单而快速的修复方法,因为我只需要跨域请求来进行开发。结果我只需要
在 Apache 上的 PHP 脚本中设置两者。然后在我的 JavaScript 代码中:
这就成功了
OK I figured it out. I was looking for a simple and quick fix since I only need the cross domain requests for development purposes. Turns out that I just had to set both
In my PHP script on Apache. Then in my JavaScript code:
And that did the trick
使用网络服务器的反向代理功能来代理 http://foo.bar.no/API/map_tools /clean_addresses/check 到 http://foo.bar.no:9294/API/map_tools/clean_addresses/check。
添加类似内容
因此,当您使用 Apache 时,您应该在虚拟主机配置中
Use your webserver's reverse proxy capabilities to proxy http://foo.bar.no/API/map_tools/clean_addresses/check to http://foo.bar.no:9294/API/map_tools/clean_addresses/check.
So, as you use Apache, you should add something like
to your vhost config
您可以通过使用
--disable-web-security
标志启动 Chrome 来绕过跨域安全限制。例如(在 OS X 上):
You can get around cross-domain security restrictions in chrome by starting it with the
--disable-web-security
flag.E.g. (on OS X):