0

我偶尔会看到失败的 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. 服务器是谷歌应用引擎

这是 .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
  }
}

谢谢,

汤姆

4

1 回答 1

1

似乎 statusCode 0 表示即使没有发送标头也表示响应为空,这很难弄清楚,您需要自己找出来

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

相反,您应该使用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
于 2013-03-31T20:35:36.700 回答