如何检测 dojo 中的请求超时错误?由于 dojo/request/notify 没有检测到它

How to detect request timeout errors in dojo? As dojo/request/notify does not detect it

我正在使用 dojo/request/notify 以便在发现 Ajax 调用的问题时全局检测错误。

为了调用 API,我正在使用 dojo/store/JsonRest。 当请求失败时 notify 按预期工作,但在超时的情况下 notify 不工作。

我需要检测超时错误。

dojo 似乎没有提供开箱即用的选项。

我想到了以下解决方案:

  1. 编写 dojo/store/JsonRest 的自定义版本,它传递 timeout 值。
  2. 猴子补丁dojo/_base/xhr.
  3. 金钱补丁XMLHttpRequest.

我选择了解决方案 3。下面是一个代码示例:

 (function (xhr) {
        var send = xhr.send;
        xhr.send = function (data) {
            this.timeout = 5000;
            var hasTimeOut = 'ontimeout' in this;
            if (hasTimeOut) {
                this.ontimeout = function () {
                    throw ('Error XMLHttpRequest timeout.');
                };
            }
            return send.apply(this, arguments);
        };
    })(XMLHttpRequest.prototype);

.