【问题标题】:Access to restricted URI denied" code: "1012访问受限 URI 被拒绝”代码:“1012
【发布时间】: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 中传递用户名+密码。

标签: ajax firefox


【解决方案1】:

使用 JQuery 1.5+ 很容易,我建议您将其用于 JavaScript 解决方案:

$.ajax({
    url: 'http://admin:admin@localhost/rest/service',
    crossDomain:true, // Here is the JSONP callback logic 
    success: function(data){
        console.log(data); // data is what comes back from your remote file
    }
});

【讨论】:

  • 非常感谢!我没有意识到它会这么简单,每当我看到这样的事情之前,他们总是谈论回调和需要服务器端代码等。
  • 以前是这样的。 jQuery 团队摇滚 :)
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2012-03-17
相关资源
最近更新 更多