XMLHttpRequest状态为0的哪个回调?

时间:2013-03-31 18:10:58

标签: google-app-engine google-chrome xmlhttprequest

我偶尔会看到一个失败的XMLHttpRequest.send()。从Chrome的网络面板我看到状态为0 - 请参阅下面的.har文件。运行send()的代码在99%的时间内成功,但偶尔(~1 / 300)它返回0。

我的问题是:我怎么抓住这个?是否有回调会抓住它?

我目前正在使用onload和onerror:

var xhr = new XMLHttpRequest();
xhr.onload = function(){};
xhr.onerror = function(){};

这两个都没有被调用,所以它无声地失败了。 还有一些需要注意的事项:

  1. 服务器日志中没有提示请求的证据
  2. Chrome控制台中没有错误消息
  3. 服务器是谷歌应用引擎
  4. 这是.har文件输出。 ## =节录。

    {
      "startedDateTime": "2013-03-30T23:52:20.972Z",
      "time": 97,
      "request": {
        "method": "GET",
        "url": "https://www.#######.com/project/auth_upload?to_sign=PUT%0A%0A%0A%0Ax-amz-date%3ASat%2C%2030%20Mar%202013%2023%3A52%3A20%20GMT%0A/s3.#####.com/######.mp4%3FpartNumber%3D50%26uploadId%3D#################.w--&asset_id=#############&project_id=###########",
        "httpVersion": "HTTP/1.1",
        "headers": [
          {
            "name": "Referer",
            "value": "https://www.######.com/console/###########"
          },
          {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22"
          }
        ],
        "queryString": [
          {
            "name": "to_sign",
            "value": "PUT%0A%0A%0A%0Ax-amz-date%3ASat%2C%2030%20Mar%202013%2023%3A52%3A20%20GMT%0A/s3.#######.com/############.mp4%3FpartNumber%3D50%26uploadId%3D##############"
          },
          {
            "name": "asset_id",
            "value": "###########"
          },
          {
            "name": "project_id",
            "value": "###############"
          }
        ],
        "cookies": [],
        "headersSize": 595,
        "bodySize": 0
      },
      "response": {
        "status": 0,
        "statusText": "",
        "httpVersion": "HTTP/1.1",
        "headers": [],
        "cookies": [],
        "content": {
          "size": 0,
          "compression": 0
        },
        "redirectURL": "",
        "headersSize": 13,
        "bodySize": 0
      },
      "cache": {},
      "timings": {
        "blocked": 0,
        "dns": -1,
        "connect": -1,
        "send": -1,
        "wait": -1,
        "receive": null,
        "ssl": -1
      }
    }
    

    谢谢,

    汤姆

1 个答案:

答案 0 :(得分:1)

似乎statusCode 0表示响应为空甚至标题都没有发送,这是很难弄清楚的,你需要自己找出它

但是您说响应没有到达您的函数是因为您正在使用loaderror事件来侦听响应,实际上您不应该完全依赖于这两个错误,考虑在请求完成之前发生超时的情况,然后'timeout'事件将被触发而不是'错误'

相反,您应该使用readystatechange事件,该事件将在每次状态更改请求时被调用,您还可以跟踪与未收到响应相关的超时或错误

httpRequest.onreadystatechange = stateChangeHandler;

stateChangeHandler = function() {
      // The readyState can be 4 values:
      //  0 - uninitialized
      //  1 - loading
      //  2 - loaded
      //  3 - interactive
      //  4 - complete
      //
      // readyState 0 - 3 can be completely ignored by us, as they are only updates
      // about the current progress. Only on readyState 4, should we continue and
      // start checking for the response status.
      if (xmlHttpRequest.readyState != 4) {
            return;
      }

      // Check HTTP Response code
      if (xmlHttpRequest.status != 200) {
            // response is ok process it
      } else {
            // there was some error
      }
}

<强>参考:

  1. https://developer.mozilla.org/en-US/docs/HTTP#HTTP_Response_Codes
  2. http://www.w3.org/TR/XMLHttpRequest/#event-xhr-readystatechange
  3. http://msdn.microsoft.com/en-us//library/ms534361%28en-us,VS.85%29.aspx
  4. http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute