简体   繁体   English

IE11 XMLRequest访问被拒绝

[英]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. 我尝试在http://localhost:8080/上执行一个简单的AJAX请求,并且在使用IE 11时立即收到错误消息。我认为IE甚至都没有尝试发送任何内容。

Did somebody meet this issue? 有人遇到这个问题了吗?

Here is a fiddle showing this behavior, 这是显示这种行为的小提琴

html: 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. 在尝试之前,您将需要启动IE F12开发人员工具,并且您将看到IE捕获到异常。

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

It doesn't work because you are referencing an object named _xhr that does not exist within the scope of the onReadyState function. 它不起作用,因为您引用的是_xhr对象,该对象在onReadyState函数的范围内不存在。

You should be using this instead : 您应该使用this来代替:

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. 这是因为XMLHttpRequest对象将使用其自己的上下文来回调onReadyState ,可以通过您的函数this进行访问。

Also note that the onReadyState function misses a semi-colon at the end of its definition, didn't notice it at first sight. 还要注意, onReadyState函数在其定义末尾onReadyState分号, onReadyState没有注意到它。

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. 编辑:我还注意到IE10(和IE11)确实将某些HTTP响应代码解释网络错误 (例如401响应代码),如果是您的情况,则IE检索资源失败很有意义。

I forked your fiddle and wrote a simple page that works well with IE11. 我派遣了您的小提琴,并编写了一个简单的页面 ,该页面与IE11兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM