XDomainRequest open("get", url)在IE中给出访问拒绝错误

XDomainRequest open("get", url) gives Access Denied error in IE

本文关键字:quot 访问 IE 错误 拒绝 url open get XDomainRequest      更新时间:2023-09-26

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

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

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

SCRIPT5: Access is denied.

然后我想检查浏览器类型,并为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控制台获得拒绝访问错误?

试试下面的…它在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);
相关文章: