代码之家  ›  专栏  ›  技术社区  ›  Matt Smith

XmlHttpRequest.onload在边缘浏览器中不工作

  •  3
  • Matt Smith  · 技术社区  · 8 年前

    XmlHttpRequest.onload . 但它确实可以在Chrome、Firefox和Safari中执行。

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

    onload

    2 回复  |  直到 8 年前
        1
  •  0
  •   Dave B    8 年前

    在发送XHR并返回响应之前,不应调用onload函数(除非出现错误)。你在打电话吗xhr.发送()?

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

        2
  •  0
  •   Bob Fanger    8 年前

    放置 xhr.responseType = "json" xhr.open(...)

    因为 xhr.responseType 中的第三个参数确定的同步请求不允许 open() 打电话。

    等等,那是在IE11里的,我试过你在Edge里的片段,运行得很好。

    尝试添加

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

    查看是否发生错误。