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

查看:382
本文介绍了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.

任何帮助将非常感谢。

谢谢! >

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


$ b onReadyState 具有自己的上下文,可以通过 this 在您的函数中访问。

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.

EDIT:我也注意到IE10(和IE11)的确解释了一些HTTP响应代码作为网络错误 (例如使用 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天全站免登陆