XDomainRequest open(“get”,url)在IE中提供Access Denied错误

时间:2013-06-13 11:52:23

标签: javascript internet-explorer xmlhttprequest access-denied xdomainrequest

我在我的一个JavaScript文件中使用以下代码。

xhr = new XMLHttpRequest();
xhr.open("GET", dest, true); // dest is the URL
xhr.onreadystatechange = checkData;
xhr.send(null);

但是当我在IE中运行脚本时,它会给我以下错误。

  

SCRIPT5:访问被拒绝。

然后我想检查浏览器类型并为IE执行单独的代码,如下所示。

if(isIE){
    xhr = new XDomainRequest(); 
    xhr.onerror = function (res) { alert("error: " + res); };
    xhr.ontimeout = function (res) { alert("timeout: " + res); };
    xhr.onprogress = function (res) { alert("on progress: " + res); };
    xhr.onload = function (res) { alert("on load: " + res); };
    xhr.timeout = 5000;
    xhr.open("get", dest); // Error line
    xhr.send(json);
}

但是我再次使用以下代码

给出了同样的错误
xhr.open("get", dest);

最后,我想调用checkData函数,就像我在下面与其他浏览器一样。

xhr.onreadystatechange = checkData;

我在那里错过了在IE控制台上获得“拒绝访问”的错误?

1 个答案:

答案 0 :(得分:0)

尝试以下..它在IE8和IE9中适用于我。

 if ($.browser.msie && window.XDomainRequest) {
                // Use Microsoft XDR
               var xdr = new XDomainRequest();
               xdr.open("get", serviceURL+'GetItem/50000');
               xdr.onload = function() {
                    //parse response as JSON
                   var response1 = $.parseJSON(xdr.responseText);
                   if (response1 == null || typeof(response1) == 'undefined') {
                        var result = $.parseJSON(data.firstChild.textContent);
                        alert(result);
                    } else {
                        $(response1.ResultData).each(function(i, item) {
                            alert(item.BorrName.toString());
                        });
                    }
                 };
               xdr.send();
               xdr.onerror = function(errormsg) {
                            alert('in error');
                 };
            }

服务如下所示

[OperationContract]
        [WebGet(UriTemplate = "GetItem/{itemnumber}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        ItemEntity GetItem(string itemnumber);
相关问题