XMLHttpRequest抛出InvalidStateError,说“必须打开对象状态”

时间:2015-02-15 08:54:55

标签: javascript ajax xmlhttprequest

代码 -

"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);
        }
    };

};

抛出以下错误:

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

如何使用

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();

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

2 个答案:

答案 0 :(得分:39)

错误很简单:

  

contextMenus的事件处理程序出错:InvalidStateError:无法执行' setRequestHeader' on' XMLHttpRequest':对象的状态必须打开。

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



鉴于您的代码,我认为最好的方法是将调用移至init(..)函数中打开。

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);
        }
    };

};

答案 1 :(得分:3)

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

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

我认为你需要这样做:

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

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