InvalidStateError - Kendo UI 上传

InvalidStateError - Kendo UI Upload

当我尝试设置我的授权 header 时,我不断收到这个奇怪的错误,我不断收到 'InvalidStateError'。这是我的代码:

$("#files").kendoUpload({
                        async: {
                            saveUrl: myApiUrl + "/" + id,
                            autoUpload: true
                        },
                        upload: function(e) {
                            var xhr = e.XMLHttpRequest;
                            if (xhr) {
                                xhr.addEventListener("readystatechange", function onReady(e) {
                                    if (xhr.readyState === 1 /* OPENED */) {
                                            xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
                                    }
                                });
                            }
                        }
                    });

事实证明,IE 出于某种原因为 readyState==1 触发了 readystatechange 两次。我不知道为什么,但确实如此。这是它第二次调用它导致它抛出错误。所以这是我的解决方案:

第一次调用后,我就把监听器去掉了。

$("#files").kendoUpload({
                        async: {
                            saveUrl: myApiUrl + "/" + id,
                            autoUpload: true
                        },
                        upload: function(e) {
                            var xhr = e.XMLHttpRequest;
                            if (xhr) {
                                xhr.addEventListener("readystatechange", function onReady(e) {
                                    if (xhr.readyState === 1 /* OPENED */) {
                                        xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
                                        xhr.removeEventListener("readystatechange", onReady);
                                    }
                                });
                            }
                        }
                    });