简体   繁体   English

XmlHttpRequest.onload无法在Edge浏览器中运行

[英]XmlHttpRequest.onload not working in Edge browser

I'm running into an issue where Microsoft Edge browser isn't able to execute XmlHttpRequest.onload . 我遇到了Microsoft Edge浏览器无法执行XmlHttpRequest.onload But it does execute in Chrome, Firefox, and Safari. 但它确实在Chrome,Firefox和Safari中执行。

I'm creating an XmlHttpRequest object: 我正在创建一个XmlHttpRequest对象:

var xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.open("POST", authenticateUrl, true);
xhr.setRequestHeader("X-OpenAM-Username", email);
xhr.setRequestHeader("X-OpenAM-Password", password);
xhr.setRequestHeader("Content-Type", "application/json");

alert("XHR object is created"); 

xhr.onload = function () {
  alert("start onload...");

  //...receives response from service...
}

xhr.send();

The alerts in place to validate it's reaching a specific line of code. 有适当的警报来验证它是否达到了特定的代码行。 After the XMLHttpRequest object is created, the code goes into the onload function. 创建XMLHttpRequest对象后,代码将进入onload函数。 Microsoft Edge won't go into it. Microsoft Edge不会进入它。 As I said, Chrome, Firefox, and Safari execute this code fine. 正如我所说,Chrome,Firefox和Safari执行此代码很好。

Is there something additional needed for making Microsoft Edge run this code? 是否还有其他需要使Microsoft Edge运行此代码?

The onload function shouldn't be called until you send the XHR and the response comes back (unless there is an error). 在发送XHR并且响应返回之前不应调用onload函数(除非出现错误)。 Are you calling xhr.send()? 你在调用xhr.send()吗?

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onload https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onload

Place the xhr.responseType = "json" line after the xhr.open(...) xhr.open(...)之后放置xhr.responseType = "json"

Because the xhr.responseType is not allowed for synchronous requests which is determined by the third parameter in the open() call. 因为同步请求不允许使用xhr.responseType ,这是由open()调用中的第三个参数确定的。

Wait, that was in IE11, i've tried your snippet in Edge and it ran just fine. 等等,那是在IE11中,我已经在Edge中尝试了你的片段,它运行得很好。

Try adding a 尝试添加一个

xhr.onerror = err => alert('Error:' + err.message);

to see if an error occurred. 查看是否发生了错误。

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

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