Javascript XMLHttpRequest 抛出 InvalidSateError 说“必须打开对象状态”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28524485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:56:24  来源:igfitidea点击:

XMLHttpRequest throwing InvalidSateError saying "Object state must be opened"

javascriptajaxxmlhttprequest

提问by SachinGutte

The code -

编码 -

"use strict";

var AJAX = function (params) {
    this.server ={};
    this.url = params.url;
    this.method = params.method;
    this.dataType = params.dataType;
    this.formData = params.formData;

    this.init = function(){
        if(typeof XMLHttpRequest != 'undefined'){
            this.server = new XMLHttpRequest();
            this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            this.server.setRequestHeader('Content-length', this.formData.length);
            this.server.setRequestHeader('Connection', 'close');
            console.log("XMLHttpRequest created.");
            return true;
        }
    };

    this.send = function(){
        if(this.init()){
            this.server.open(this.method, this.url, true);
            this.server.send(this.formData);
        }
    };

};

It is throwing following error :

它抛出以下错误:

Error in event handler for contextMenus: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.

How it's being used -

它是如何使用的 -

var data = new FormData();

data.append('user', 'sachin');
var params = {
    url : 'example.com',
    method : 'post',
    dataType: 'json',
    formData : data
};

var backgroundWindow = chrome.extension.getBackgroundPage();

var ajax = new backgroundWindow.AJAX(params);

ajax.send();

I can't seem to figure out what's the reason behind.

我似乎无法弄清楚背后的原因是什么。

回答by UltraInstinct

The error is straight forward:

错误很简单:

Error in event handler for contextMenus: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.

contextMenus 的事件处理程序出错:InvalidStateError:无法在“XMLHttpRequest”上执行“setRequestHeader”:对象的状态必须为 OPENED。

You need to call .open(..)before setting the request headers.

您需要.open(..)在设置请求标头之前调用。





Given your code, I believe the best way would be to move the call to open in the init(..)function.

鉴于您的代码,我相信最好的方法是将调用移动到init(..)函数中的open 。

var AJAX = function (params) {
    this.server ={};
    this.url = params.url;
    this.method = params.method;
    this.dataType = params.dataType;
    this.formData = params.formData;

    this.init = function(){
        if(typeof XMLHttpRequest != 'undefined'){
            this.server = new XMLHttpRequest();

            //Open first, before setting the request headers.
            this.server.open(this.method, this.url, true);

            //Now set the request headers.
            this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            //this.server.setRequestHeader('Content-length', this.formData.length);
            //this.server.setRequestHeader('Connection', 'close');
            console.log("XMLHttpRequest created.");
            return true;
        }
    };

    this.send = function(){
        if(this.init()){
            this.server.send(this.formData);
        }
    };

};

回答by Ganesh Kamath - 'Code Frenzy'

You probably need to open the connection after the XMLHttpRequestcall and before the setRequestHeadercall Instead of:

您可能需要在XMLHttpRequest通话之后和通话之前打开连接,setRequestHeader而不是:

        this.server = new XMLHttpRequest();
        this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

i think you need to do:

我认为你需要这样做:

        this.server = new XMLHttpRequest();
        this.server.open(this.method, this.url, true);
        this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

also make sure you remove this line in the init(), like you have done above.

还要确保删除 中的这一行init(),就像上面所做的那样。