gpt4 book ai didi

javascript - IE11 XMLRequest 访问被拒绝

转载 作者:行者123 更新时间:2023-11-30 17:28:19 25 4
gpt4 key购买 nike

我尝试在 http://localhost:8080/ 上执行一个简单的 AJAX 请求,但在使用 IE 11 时我立即收到错误消息。我认为 IE 甚至没有尝试发送任何东西。

有人遇到过这个问题吗?

这是一个fiddle表现出这种行为,

html:

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

加载时:

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 捕获异常。

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

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

你应该使用 this 来代替:

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

那是因为 XMLHttpRequest 对象会用它自己的上下文回调 onReadyState,可以通过函数中的 this 访问它。

另请注意,onReadyState 函数在其定义的末尾缺少一个分号,第一眼没注意到。

编辑:我还注意到 IE10(和 IE11)确实解释了 some HTTP response code as network errors (例如使用 401 响应代码),如果是您的情况,那么 IE 无法检索您的资源是有道理的。

我 fork 了你的 fiddle 和 wrote a simple page这适用于 IE11。

关于javascript - IE11 XMLRequest 访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com