我尝试在http://localhost:8080/上执行简单的AJAX请求,使用IE 11时我立即收到错误消息。我认为IE甚至都没有尝试发送任何内容。

有人遇到这个问题了吗?

这是显示此行为的fiddle

的HTML:

<button id='btn1'>launch</button>


负载:

var xhr,btn=document.getElementById('btn1');
btn.onclick=onButton;

var onReadyState=function(){
  console.log('ready state:'+_xhr.readyState);
  if(xhr.readyState===4){
    console.log('status:'+_xhr.status);
  }
}

function onButton(){
  xhr=new window.XMLHttpRequest();
  xhr.onreadystatechange=onReadyState;
  xhr.open('POST','http://localhost:8080/ScanAPI/v1/client');
  xhr.send();
}


在尝试之前,您将需要启动IE F12开发人员工具,并且您将看到IE捕获到异常。

任何帮助,将不胜感激。

谢谢!

最佳答案

它不起作用,因为您引用的是_xhr函数范围内不存在的名为onReadyState的对象。

您应该使用this代替:

var onReadyState = function() {
    if (this.readyState === 4) {
        console.log('status :' + this.status);
    }
};


这是因为XMLHttpRequest对象将使用自己的上下文回调onReadyState,可以通过函数中的this对其进行访问。

另请注意,onReadyState函数在其定义末尾遗漏了分号,乍一没有注意到它。

编辑:我还注意到IE10(和IE11)确实解释了some HTTP response code as network errors(例如使用401响应代码),如果您的情况如此,那么IE无法检索您的资源就很有意义。

我对您的提琴和wrote a simple page进行了分叉,使其与IE11一起正常工作。

关于javascript - IE11 XMLRequest访问被拒绝,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23771002/

10-12 01:44
查看更多