有什么方法可以通过 Access-Control-Allow-Origin 在我自己的服务器上进行开发吗?

发布于 2024-12-25 20:46:36 字数 826 浏览 1 评论 0原文

我收到此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦旅人picnic 2025-01-01 20:46:37

好吧,我想通了。我一直在寻找一种简单而快速的修复方法,因为我只需要跨域请求来进行开发。结果我只需要

header("Access-Control-Allow-Origin: http://foo.bar.no:9294");
header("Access-Control-Allow-Credentials: true");

在 Apache 上的 PHP 脚本中设置两者。然后在我的 JavaScript 代码中:

# Set jQuery ajax to use 'withCredentials' globally
$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    }
});

这就成功了

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

header("Access-Control-Allow-Origin: http://foo.bar.no:9294");
header("Access-Control-Allow-Credentials: true");

In my PHP script on Apache. Then in my JavaScript code:

# Set jQuery ajax to use 'withCredentials' globally
$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    }
});

And that did the trick

唱一曲作罢 2025-01-01 20:46:37

使用网络服务器的反向代理功能来代理 http://foo.bar.no/API/map_tools /clean_addresses/checkhttp://foo.bar.no:9294/API/map_tools/clean_addresses/check

添加类似内容

    <Proxy *>
            Order allow,deny
            allow from all
    </Proxy>

    ProxyPass /API/map_tools/ http://foo.bar.no:9294/API/map_tools/
    ProxyPassReverse /API/map_tools/ http://foo.bar.no:9294/API/map_tools/

因此,当您使用 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

    <Proxy *>
            Order allow,deny
            allow from all
    </Proxy>

    ProxyPass /API/map_tools/ http://foo.bar.no:9294/API/map_tools/
    ProxyPassReverse /API/map_tools/ http://foo.bar.no:9294/API/map_tools/

to your vhost config

誰ツ都不明白 2025-01-01 20:46:37

您可以通过使用 --disable-web-security 标志启动 Chrome 来绕过跨域安全限制。

例如(在 OS X 上):

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

You can get around cross-domain security restrictions in chrome by starting it with the --disable-web-security flag.

E.g. (on OS X):

open /Applications/Google\ Chrome.app/ --args --disable-web-security
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文