Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Request Headers to the XmlHttpRequest with Kendo UI Upload control

I'm trying to use the code provided by Telerik to add request headers to the XHR, but I'm getting the following error.

"0x800a139e - JavaScript runtime error: InvalidStateError"

Here is the code

$(document).ready(function () {
                        $("#file").kendoUpload({
                            multiple: false,
                            async: {
                                saveUrl: "save",
                            },
                            upload: onUpload,

                        });
                    });

function onUpload(e) {
                        var xhr = e.XMLHttpRequest;
                        if (xhr) {
                            xhr.addEventListener("readystatechange", function (e) {
                                if (xhr.readyState == 1 /* OPENED */) {
                                    xhr.setRequestHeader("X-Foo", "Bar");
                                }
                            });
                        }
                    }
like image 792
johnway2 Avatar asked Mar 18 '26 04:03

johnway2


1 Answers

It turned out that Internet Explorer fires readystatechange twice with readyState 1 and the second execution of the handler causes the error. As a workaround for the the current case you could name the executed handler and unbind it after the first execution.

function onUpload(e) {
                    var xhr = e.XMLHttpRequest;
                    if (xhr) {
                        xhr.addEventListener("readystatechange", function onUploadReady(e) {
                            if (xhr.readyState == 1 /* OPENED */) {
                                xhr.setRequestHeader("X-Foo", "Bar");
                                xhr.removeEventListener("readystatechange", onUploadReady);
                            }
                        });
                    }
                }
like image 92
Marek Pavelek Avatar answered Mar 20 '26 18:03

Marek Pavelek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!