可以使用 xhr.onreadystatechange 属性指向的函数去监听 xhr.readyState 值的变化. 示例如下: 

var xhr = new XMLHttpRequest();
xhr.open( 'GET', 'http://example.com' , true );
xhr.onreadystatechange = function () {
  if (xhr.readyState !== 4 || xhr.status !== 200) {
    return;
  }
  console.log(xhr.responseText);
};
xhr.send();

 

注意: 除了xhr的正常请求过程会改变 xhr.readyState 值以外, 类似xhr.abort()这种会终止请求的方法也会改变xhr.readyState的值.

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-02
  • 2021-10-28
  • 2021-07-23
  • 2021-10-18
猜你喜欢
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
相关资源
相似解决方案