XMLHttpRequest throwing InvalidSateError saying “Object state must be opened”

前端 未结 2 1519
悲哀的现实
悲哀的现实 2020-12-14 16:26

The code -

\"use strict\";

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


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 17:07

    You probably need to open the connection after the XMLHttpRequest call and before the setRequestHeader call Instead of:

            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.

提交回复
热议问题