IE11 XMLRequest Access is Denied

雨燕双飞 提交于 2019-12-24 12:44:17

问题


I tried to do a simple AJAX request on http://localhost:8080/ and I get an error right away when using IE 11. I don't think IE is even trying to send anything.

Did somebody meet this issue?

Here is a fiddle showing this behavior,

html:

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

onLoad:

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();
}

You will need to launch the IE F12 developer tool, before trying and you will see IE catching the exception.

Any help on this would be greatly appreciated.

Thanks!


回答1:


It doesn't work because you are referencing an object named _xhr that does not exist within the scope of the onReadyState function.

You should be using this instead :

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

That's because the XMLHttpRequest object will call back onReadyState with its own context, which is accessible through this in your function.

Also note that the onReadyState function misses a semi-colon at the end of its definition, didn't notice it at first sight.

EDIT : I also noticed that IE10 (and IE11) does interpret some HTTP response code as network errors (such as with a 401 response code), if it is your case, then it makes sense that IE fails at retrieving your resource.

I forked your fiddle and wrote a simple page that works well with IE11.



来源:https://stackoverflow.com/questions/23771002/ie11-xmlrequest-access-is-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!