📄 utilxmlhttp.js
字号:
var _XMLHttp=[];
function XMLHttp(callback) {
this.callback=callback;
this.callbackData=null;
this.query='';
this.content='';
this.url='';
this.userUrl='';
this.async=true;
this.data=null;
this._debug=false;
this.xmlHttpReq=(window.XMLHttpRequest)?new XMLHttpRequest():(window.ActiveXObject?new ActiveXObject('Microsoft.XMLHTTP'):null);
this.setCallback = function (callback) {this.callback=callback;};
this.setCallbackData = function (data) {this.callbackData=data;};
this.setData = function (data) {this.data=data;};
this.setQuery = function (query) {this.query=query;};
this.setUrl = function (url) {this.url=url;};
this.setUserUrl = function (url) {this.userUrl=url;};
this.setAsync = function (async) {this.async=async;};
this.setHeader = function (label, value) {this.xmlHttpReq.setRequestHeader(label, value);};
this.debug = function () {open(this.requestURI())};
this.requestURI = function () {
if (this.userUrl) return this.userUrl;
return '/'+_dbName;
};
this.executed = false;
this.open = function () {
var _url=this.requestURI();
if (this._debug) {
parent.open(_url);
return false;
}else{
this.xmlHttpReq.open('POST', _url, this.async);
return true;
}
};
this.resetContent = function () {this.content='';};
this.setContent = function (content) {this.content+=content;};
this.getContent = function () {return this.content;};
this.send = function (content) {
if (!content)
content=this.content;
if (content) {
this.setHeader('Content-Type', 'application/x-www-form-urlencoded');
this.setHeader('Content-Length', content.length);
this.xmlHttpReq.send(content);
this.executed=true;
}else{
this.setHeader('Content-Length', '0');
this.xmlHttpReq.send(null);
this.executed=true;
}
};
this.registCallback = function (callback) {
if (callback) {
var argument=this.callbackData;
this.xmlHttpReq.onreadystatechange=function() {callback(argument);};
}else this.xmlHttpReq.onreadystatechange=function() {};
};
this.execute = function (content) {
if (this.xmlHttpReq) {
if (this.xmlHttpReq.readyState > 0 || this.executed) this.abort();
this.registCallback(this.callback);
if (this.open()) {
this.send(content);
if (!this.async && this.callback) this.callback(this);
}
}else
alert('Cannot execute.');
};
this.success = function () {
if (!this.async) return this.xmlHttpReq;
return (this.xmlHttpReq && this.xmlHttpReq.readyState == 4 && (this.xmlHttpReq.status == 200 || this.xmlHttpReq.status == 202));
};
this.working = function () {
if (this.xmlHttpReq) return this.xmlHttpReq.readyState;
else return 0;
};
this.status = function () {
return this.xmlHttpReq.status;
};
this.getTree = function () {
if (!this.xmlHttpReq || !this.xmlHttpReq.responseXML || !this.xmlHttpReq.responseXML.documentElement) return null;
var tree={};
var root=this.xmlHttpReq.responseXML.documentElement;
for (var node=root.firstChild; node; node=node.nextSibling) {
var data=this._getTree(node);
if (tree[node.nodeName])
tree[node.nodeName].push(data);
else
tree[node.nodeName]=[data];
}
return tree;
};
this._getTree = function (node) {
if (!node) return false;
var ret=new Object;
var data;
for (var i=0; i < node.childNodes.length; i++) {
if (node.childNodes[i].hasChildNodes() && node.childNodes[i].firstChild.hasChildNodes())
data=this._getTree(node.childNodes[i]);
else
data=(_isIE?node.childNodes[i].text:node.childNodes[i].textContent);
var name=node.childNodes[i].nodeName;//.toLowerCase();
if (name == '#text') {
ret=data;
}else{
if (ret[name])
ret[name].push(data);
else
ret[name]=[data];
}
}
return ret;
};
this.doc = function (rootNodeName) {
if (!rootNodeName) rootNodeName='RESPONSE';
if (!this.xmlHttpReq || !this.xmlHttpReq.responseXML || !this.xmlHttpReq.responseXML.documentElement) return null;
return (this.xmlHttpReq.responseXML.documentElement.nodeName == rootNodeName)?this.xmlHttpReq.responseXML.documentElement:null;
};
this.getResponse = function (nodeName) {
var ret=[];
if (this.success()) {
var root=this.doc();
if (root && root.nodeName == 'RESPONSE' && root.firstChild.nodeName == nodeName) {
for (var node=root.firstChild; node && node.nodeName == nodeName && node.hasChildNodes(); node=node.nextSibling) {
var data=this.getRow(node);
ret.push(data);
}
return ret;
}else return false;
}else return false;
};
this.getError = function (type) {
if (this.success()) {
var root=this.doc();
if (root && root.nodeName == 'RESPONSE' && root.firstChild.nodeName == 'ERROR') {
for (var node=root.firstChild; node && node.nodeName == 'ERROR' && node.hasChildNodes(); node=node.nextSibling) {
var data=this.getRow(node);
if (type == 'code') return data.errorcode;
else if (type == 'msg') return data.errormsg;
else return data;
}
return false;
}else return false;
}else return false;
};
this.getRow = function (node) {
if (!node) return false;
var ret=new Object;
for (var i=node.childNodes.length; i--;) {
if (node.childNodes[i].hasChildNodes() && node.childNodes[i].firstChild.hasChildNodes())
ret[node.childNodes[i].nodeName/*.toLowerCase()*/]=this.getRow(node.childNodes[i]);
else
ret[node.childNodes[i].nodeName/*.toLowerCase()*/]=(_isIE?node.childNodes[i].text:node.childNodes[i].textContent);
}
return ret;
};
this.xml = function () {
if (this.success() && this.xmlHttpReq && this.xmlHttpReq.responseXML)
return this.xmlHttpReq.responseXML.xml;
else return '';
};
this.headers = function () {
return this.xmlHttpReq.getAllResponseHeaders();
};
this.text = function () {
if (this.success() && this.xmlHttpReq && this.xmlHttpReq.responseText)
return this.xmlHttpReq.responseText;
else return '';
};
this.abort = function () {if (this.xmlHttpReq) {this.xmlHttpReq.abort();this.executed=false;}};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -