【问题标题】:why XMLHttpRequestEventTarget.onerror doesn't work in synchronous request?为什么 XMLHttpRequestEventTarget.onerror 在同步请求中不起作用?
【发布时间】:2019-07-18 19:28:37
【问题描述】:

如果请求失败,我尝试使用XMLHttpRequestEventTarget.onerror 处理,但它仅适用于异步请求.. 虽然我确实需要同步请求,因为它使代码对我来说更容易并且可以按顺序工作。 代码

const http = new XMLHttpRequest();
const url = 'http://192.168.1.2/';
http.open('get', url, false ); 



  http.onreadystatechange = function ()
    {
        if(http.readyState === 4)
        {
            if(http.status === 200)
            {

console.log("it worked ")

            }

        }
    }

   http.onerror = function () {
  console.log("** An error occurred during the transaction");
  };


http.send();

enter image description here

【问题讨论】:

    标签: javascript ajax asynchronous xmlhttprequest synchronization


    【解决方案1】:

    只需将其转回 aync(true) 并使用回调修复其余代码!

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 2020-09-19
      • 2021-05-20
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多