XMLHttpRequest throwing InvalidSateError saying quot;Object state must be openedquot;(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 说“必须打开对象状态";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:XMLHttpRequest 抛出 InvalidSateError 说“必须打开对象状态";
基础教程推荐
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01