IE11 XMLRequest访问被拒绝 [英] IE11 XMLRequest Access is Denied

查看:1227
本文介绍了IE11 XMLRequest访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 http:// localhost:8080 / 上做一个简单的AJAX请求,并且在使用IE 11时立即收到错误。我不认为IE甚至试图发送任何东西。

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.

有人遇到这个问题吗?

这是一个小提琴显示此行为,

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

您将需要启动IE F12开发人员工具,然后尝试,您将请参阅IE捕获异常。

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!

推荐答案

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

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

您应该使用 / code>代替:

You should be using this instead :

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

这是因为 XMLHttpRequest onReadyState 具有自己的上下文,可以通过您的函数中的访问。

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

另请注意, onReadyState 功能在其定义的末尾缺少分号,并没有注意到它的一见。

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

编辑:我也注意到IE10(和IE11)解释了网络错误 的状态0替代401-in-ie-10(例如使用 401 响应代码),如果是这种情况,那么IE检索您的资源就失败了。

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.

我分叉了你的小提琴,写了一个简单的页面,与IE11一起使用。

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

这篇关于IE11 XMLRequest访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆