XMLHttpRequest抛出InvalidSateError说“;对象状态必须打开”;
XMLHttpRequest throwing InvalidSateError saying "Object state must be opened"
代码-
"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();
我似乎不明白背后的原因是什么。
错误是直接的:
contextMenu的事件处理程序中出错:InvalidStateError:未能对"XMLHttpRequest"执行"setRequestHeader":对象的状态必须为OPENED。
在设置请求标头之前,您需要调用.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);
}
};
};
您可能需要在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()
中的这一行,就像上面所做的那样。
相关文章:
- js,对象's状态监视
- redux 中状态对象的结构
- 如何将状态对象/数据传递给(异步)ajax 回调
- 你能在js中获得以前的历史状态对象吗
- 更新HTML5历史中以前的状态对象
- stampit.js“状态”对象不是实例安全的
- 相位器.js获取给定的无效相位器状态对象
- 使用 React 创建的表单不会更新支持状态对象
- AngularJs :将 stateParams 值附加到状态对象中的自定义数据
- Redux 的 .getState() 不应该返回状态对象的副本
- Nodejs使用回调函数缓存有状态对象
- 我可以只返回状态对象的一部分而不是整个状态对象的新副本吗
- 处理一个巨大的状态对象
- 访问pushState状态对象内容
- 调用mapStateToProps似乎传递了错误的状态对象
- 试图通过将状态对象放入变量来压缩我的ui-router状态定义.并# 39;t工作
- React路由器-动态路由-通过路由器访问状态对象
- HTML5历史API-状态对象的最大大小是多少
- React:您可以将setState与现有的状态对象一起使用吗
- Pubnub:无法获得带有存在事件的状态对象