InvalidStateError - Kendo UI Upload

时间:2018-05-29 16:34:48

标签: kendo-ui kendo-upload

我不断得到这个奇怪的错误,当我尝试设置我的Authorization标头时,我不断收到'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);
                                    }
                                });
                            }
                        }
                    });

1 个答案:

答案 0 :(得分:0)

事实证明,由于某种原因,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);
                                    }
                                });
                            }
                        }
                    });