【发布时间】:2011-07-14 22:56:31
【问题描述】:
在域 A (localhost:8080) 上,我运行此代码以访问域 B (localhost) 上的未经身份验证的 REST 服务:
req = new XMLHttpRequest();
req.open('GET', 'http://localhost/rest/service');
req.send();
这很好,我确实得到了跨域的响应,因为域 B 上的 Apache 设置了响应标头:
Header set Access-Control-Allow-Origin "http://localhost:8080"
但是,如果我现在打开 REST 服务的身份验证并尝试运行相同的请求:
req.open('GET', 'http://admin:admin@localhost/rest/service');
它现在在 Firebug 中产生这个错误:
Access to restricted URI denied" code: "1012
我很困惑,我能够绕过同源策略成功地对经过身份验证的服务进行跨域 ajax 调用,但是当服务需要身份验证时,Firefox 决定不允许 ajax 调用?我如何在不使用 jsonp 等的情况下解决此问题,因为生产服务器将无法提供 PHP 或 Servlet 托管。
【问题讨论】:
-
对于它的价值,Firefox 不允许上述的原因是因为规范禁止在跨域 XHR 的 URI 中传递用户名+密码。