【问题标题】:Access is denied. JavaScript error on request to secured page访问被拒绝。请求安全页面时出现 JavaScript 错误
【发布时间】:2011-06-20 18:00:00
【问题描述】:

在页面 SomePage.aspx,通过 JavaScript 代码 (XMLHttpRequest) 我调用 SecuredPage.aspx 使用下一个代码:

    var httpRequest = GetXmlHttp();
    var url = "https://myhost.com/SecuredPage.aspx";

    var params = "param1=" + document.getElementById('param1').value +
                "&param2=" + document.getElementById('param2').value;

    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    httpRequest.onreadystatechange = function() {
        //Call a function when the state changes.
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            alert(httpRequest.responseText);
        }
    }
    httpRequest.send(params); // HERE ACCESS IS DENIED.

    //---------------------------------------------
    function GetXmlHttp() {
        var xmlhttp = false;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        // Code for Internet Explorer.
        {
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                    xmlhttp = false;
                }
            }
        }
        return xmlhttp;
    }

它会引发 访问被拒绝 错误。如果发送到 http (http://myhost.com/SecuredPage.aspx),它工作正常。

如何解决这个问题?

【问题讨论】:

    标签: javascript ajax https xmlhttprequest


    【解决方案1】:

    如果您希望通过 Ajax 获取 HTTPS 页面,您需要从同一域上的 HTTPS 页面进行,没有其他方法,只要您使用 Ajax。这是因为the same origin policy

    也就是说,有很多方法可以不使用 Ajax,for instance you can use frames

    另一种方法是使用JSONP,但这需要您获取,嗯,JSON :)

    第三种方法,这对于生产网站来说往往不是很有用,但仍然可以很有趣地修补,是使用 YQL 作为代理。

    最后,您始终可以设置自己的服务器端代理,这样您就可以调用一个 HTTP 地址来获取 HTTPS 页面并将其发送出去,但如果可以避免,这很少是一个好的解决方案。

    【讨论】:

      【解决方案2】:

      如前所述,您的问题是您的浏览器将此视为跨域请求。适应这种情况的另一种方法是设置一个 crossdomain.xml 文件,如下所示:

      <?xml version="1.0"?>
      <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
      <cross-domain-policy>
        <allow-access-from domain="myhost.com" />
        <allow-access-from domain="ourhost.com" />
        <site-control permitted-cross-domain-policies="master-only" />
      </cross-domain-policy>
      

      不是这种方法的专家,但我已经成功地使用了它。可以通过添加更多allow-access-from 标签来添加其他域。你可能需要做一些摆弄。 YMMV。

      【讨论】:

      • 我应该在哪里放置/使用这个 XML 文件?
      • 它位于站点根目录。它应该可以在 myhost.com/crossdomain.xml 访问
      • 如果我需要myhost.com和ourhost.com这样的两个域,我应该如何将它添加到xml文件中!?非常感谢!
      • 谢谢!刚刚上传到服务器根目录,不幸的是同样的错误 - 访问被拒绝:(((
      • 我遇到了和@ihorko 一样的问题,需要再做一步才能解决。 (即使没有 crossdomain.xml 也可以在 Chrome 和朋友中使用,但 IE 并不满意。)访问forum.jquery.com/topic/cross-domain-ajax-and-ie,您可以插入一段代码来解决问题。
      【解决方案3】:

      这是因为浏览器将httphttps 视为2 个不同的站点/域,因此您必须遵守同源策略。

      Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
      

      解决它的一种方法是使用 jsonp。

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 1970-01-01
        • 2017-10-07
        • 2012-07-05
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多