EricG
EricG

Reputation: 23

IE11 XMLRequest Access is Denied

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!

Upvotes: 1

Views: 4681

Answers (1)

Halim Qarroum
Halim Qarroum

Reputation: 14103

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.

Upvotes: 2

Related Questions