向XMLHttpRequest添加标头即使将其封装在IIFE中

问题描述

this question中,我要求一种自动XMLHttpRequest对象添加标头的方法。我尝试实现this solution

(function(open) {
    XMLHttpRequest.prototype.open = function(method,url,async,user,pass) {
        this.addEventListener("readystatechange",function() {
            if (this.readyState == 4) 
            { 
                console.log(this.status);
            }
        },false);
        open.call(this,method,pass);
        this.setRequestHeader("Authorization","Token 123")
    };
})(XMLHttpRequest.prototype.open);

这在原则上是可行的,但在我的情况下却不行:我想向使用IIFE模式封装的某些vendor code添加标头:

H5PEditor.FileUploader = (function ($,Eventdispatcher) {

    // ...

    self.upload = function (file,filename) {
        var formData = new FormData();
        formData.append('file',file,filename);

        // ...

        var request = new XMLHttpRequest();

        // ...

        request.open('POST',H5PEditor.getAjaxUrl('files'),true);
        request.send(formData);
        self.trigger('upload');
    };

    // ...

})(H5P.jQuery,H5P.Eventdispatcher);

如果我在此IIFE中添加一个代码示例中的代码,则会添加标头。但是有没有一种方法可以在不更改供应商代码的情况下完成相同的任务?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)