<i id='0l7xr'><tr id='0l7xr'><dt id='0l7xr'><q id='0l7xr'><span id='0l7xr'><b id='0l7xr'><form id='0l7xr'><ins id='0l7xr'></ins><ul id='0l7xr'></ul><sub id='0l7xr'></sub></form><legend id='0l7xr'></legend><bdo id='0l7xr'><pre id='0l7xr'><center id='0l7xr'></center></pre></bdo></b><th id='0l7xr'></th></span></q></dt></tr></i><div id='0l7xr'><tfoot id='0l7xr'></tfoot><dl id='0l7xr'><fieldset id='0l7xr'></fieldset></dl></div>

      1. <small id='0l7xr'></small><noframes id='0l7xr'>

        <legend id='0l7xr'><style id='0l7xr'><dir id='0l7xr'><q id='0l7xr'></q></dir></style></legend>

          <bdo id='0l7xr'></bdo><ul id='0l7xr'></ul>
      2. <tfoot id='0l7xr'></tfoot>

        XMLHttpRequest 抛出 InvalidSateError 说“必须打开对象

        时间:2023-10-14

        <small id='i6ROv'></small><noframes id='i6ROv'>

          <tbody id='i6ROv'></tbody>
      3. <i id='i6ROv'><tr id='i6ROv'><dt id='i6ROv'><q id='i6ROv'><span id='i6ROv'><b id='i6ROv'><form id='i6ROv'><ins id='i6ROv'></ins><ul id='i6ROv'></ul><sub id='i6ROv'></sub></form><legend id='i6ROv'></legend><bdo id='i6ROv'><pre id='i6ROv'><center id='i6ROv'></center></pre></bdo></b><th id='i6ROv'></th></span></q></dt></tr></i><div id='i6ROv'><tfoot id='i6ROv'></tfoot><dl id='i6ROv'><fieldset id='i6ROv'></fieldset></dl></div>
          <legend id='i6ROv'><style id='i6ROv'><dir id='i6ROv'><q id='i6ROv'></q></dir></style></legend>

              • <bdo id='i6ROv'></bdo><ul id='i6ROv'></ul>

                  <tfoot id='i6ROv'></tfoot>
                  本文介绍了XMLHttpRequest 抛出 InvalidSateError 说“必须打开对象状态";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  代码 -

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

                  我似乎无法弄清楚背后的原因.

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

                  推荐答案

                  错误是直截了当的:

                  contextMenus 的事件处理程序出错:InvalidStateError:无法在XMLHttpRequest"上执行setRequestHeader":对象的状态必须是 OPENED.

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

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

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

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

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

                  这篇关于XMLHttpRequest 抛出 InvalidSateError 说“必须打开对象状态";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:可以用 xhr.onload 替换 xhr.onreadystatechange 来进行 下一篇:捕获 javascript XMLHttpRequest 的重定向位置

                  相关文章

                  最新文章

                  <tfoot id='OoWjo'></tfoot>

                  1. <legend id='OoWjo'><style id='OoWjo'><dir id='OoWjo'><q id='OoWjo'></q></dir></style></legend>
                    • <bdo id='OoWjo'></bdo><ul id='OoWjo'></ul>

                    <small id='OoWjo'></small><noframes id='OoWjo'>

                  2. <i id='OoWjo'><tr id='OoWjo'><dt id='OoWjo'><q id='OoWjo'><span id='OoWjo'><b id='OoWjo'><form id='OoWjo'><ins id='OoWjo'></ins><ul id='OoWjo'></ul><sub id='OoWjo'></sub></form><legend id='OoWjo'></legend><bdo id='OoWjo'><pre id='OoWjo'><center id='OoWjo'></center></pre></bdo></b><th id='OoWjo'></th></span></q></dt></tr></i><div id='OoWjo'><tfoot id='OoWjo'></tfoot><dl id='OoWjo'><fieldset id='OoWjo'></fieldset></dl></div>