IE中的XDomainRequest给出Access is Denied错误

时间:2012-11-20 17:08:01

标签: xdomainrequest

这是我正在使用的代码如下:

我使用的是IE9,无法在“网络”标签中看到发送的请求。我确实在JSP中设置了Access-Control标头:

<% response.setHeader("Access-Control-Allow-Origin", "*");%>

从JSP获取AJAX HTML内容的代码:

if ($.browser.msie && window.XDomainRequest) {

    var xdr = new window.XDomainRequest();
    xdr.open("GET", "http://dev01.org:11110/crs/qw/qw.jsp?&_=" + Math.random());
    xdr.contentType = "text/plain";
    xdr.timeout = 5000;
    xdr.onerror = function () {
        console.log('we have an error!');
    }
    xdr.onprogress = function () {
        console.log('this sucks!');
    };
    xdr.ontimeout = function () {
        console.log('it timed out!');
    };
    xdr.onopen = function () {
        console.log('we open the xdomainrequest');
    };
    xdr.onload = function() {
        alert(xdr.responseText);
    };
    xdr.send(null);
} else { ...... }

我收到Access is Denied Error。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

请求必须定位到与托管页面相同的方案

在您的示例中,您正在请求:

http://dev01 ...

你应该从HTTP协议中做到这一点。

例如: 如果您的站点是js脚本所在的位置:http://dev.org 你可以这样做:

xhr = new XDomainRequest();
xhr.open("GET", "http://dev01.org?p=1");

但是这会抛出“拒绝访问”:

xhr = new XDomainRequest();
xhr.open("GET", "https://dev01.org?p=1");

答案 1 :(得分:0)

我对XDomainRequest的体验是它不尊重Access-Control-Allow-Origin: *。相反,您必须指定域。如果您需要动态生成它,可以从HTTP_REFERER标头获取,或者如果您只想要来自一个域的请求,则可以手动设置它。 This article might help.

<% response.setHeader("Access-Control-Allow-Origin", "http://dev01.org");%>