Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

javascript - XMLHttpRequest throwing InvalidSateError saying "Object state must be opened"

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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



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

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

};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...