访问被拒绝。请求安全页面时发生 JavaScript 错误

发布于 2024-10-15 17:49:47 字数 1549 浏览 3 评论 0原文

在页面 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),它工作正常。

如何才能解决这个问题呢?

On page SomePage.aspx, by JavaScript code (XMLHttpRequest) I call SecuredPage.aspx used next code:

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

    var params = "param1=" + document.getElementById('param1').value +
                "¶m2=" + 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;
    }

It throws an Access is denied error. If send to http (http://myhost.com/SecuredPage.aspx), it works fine.

How is it possible to resolve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南七夏 2024-10-22 17:49:47

如果您希望通过 Ajax 获取 HTTPS 页面,您需要从同一个域,没有别的办法,只要用Ajax就可以了。这是因为同源政策

也就是说,有很多方法可以不使用 Ajax 来做到这一点,例如,您可以使用框架。

另一种方法是使用 JSONP,但这要求您获取 JSON

:)第三种方法,对于生产网站来说往往不是很有用,但修补起来仍然很有趣,那就是使用 YQL 作为代理。

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

If you wish to fetch an HTTPS page via Ajax you need to do it from an HTTPS page on the same domain, there is no other way, as long as you use Ajax. This is because of the same origin policy.

That said, there are plenty of ways to do this not using Ajax, for instance you can use frames.

Another way is to use JSONP, but this requires that you are fetching, well, JSON :)

A third way, that tends not to be very useful for production websites, but still can be fun to tinker around with, is to use YQL as a proxy.

Lastly you can always set up a serverside proxy of your own, so that you call an HTTP address that fetches the HTTPS page and sends it on, but this is rarely a good solution if it can be avoided.

过去的过去 2024-10-22 17:49:47

这是因为浏览器将 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。

This is because the browser considers http and https as 2 different sites/domains, and therefore you have to adhere to the same origin policy.

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.

One way to solve it is using jsonp.

冬天的雪花 2024-10-22 17:49:47

正如所说,您的问题是您的浏览器将此视为跨域请求。另一种适应这种情况的方法是设置一个 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。

As it's been said, your problem is that your browser sees this as a cross domain request. Another way to accommodate this is to set up a crossdomain.xml file like this:

<?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>

I'm not an expert on this method, but I have used it successfully. Other domains can be added by adding more allow-access-from tags. You may need to do some fiddling. YMMV.

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