📄 ajax.js
字号:
// ajax object struct
// for DMForum.NET 1.1
// 2006-12-6
function class_ajax( suri,sdata,sonComplete,sonException,smethod )
// suri:资源位置
// sdata:传送的数据
// sonComplete:完成事件,有3个参数可用.分别是xmldom:xmldom对象;this.statusCode:返回信息代码;this.status:返回信息文本.其中后面两个只在您的源根节点是result且有code和message子节点的情况下可用
// sonException:异常事件,参数1个.为异常描述文本
// smethod:传输方式
{
// ----------- publics -----------//
// source URI.
this.uri = suri;
// the request method type,default is POST
this.method = smethod;
if ( this.method == null ) this.method = "POST";
// the request's datastream
this.data = sdata;
// ajax is enabled
this.enabled = true;
// request status
this.statusCode = 0;
this.statusText = "";
// ----------- privates -----------//
// params
var myonExceprion = sonException;
// is microsoft's operasystem
var isMicrosoft = false;
// get microsoft's xmlhttpobject
var getMsObject = function()
{
var msxmls = ["MSXML3", "MSXML2", "Microsoft"];
var returnObj = null;
for (var i = 0; i < msxmls.length; i++) {
try
{
returnObj = new ActiveXObject(msxmls[i] + ".XMLHTTP");
break;
}catch (ex){}
}
return returnObj;
}
// if microsoft's object is null, try to get XMLHttpRequest object
var getObjects = function()
{
var returnObj = getMsObject();
isMicrosoft = true;
if( returnObj == null )
{
isMicrosoft = false;
try { returnObj = new XMLHttpRequest(); }
catch (ex){ this.enabled = false; }
}
return returnObj;
}
// ------------ public method ------------ //
// send the request and bind the complete handler
this.processAnsyc = function( uri,callback )
{
var ajaxer = getObjects();
if ( !this.enabled ) return false;
try
{
ajaxer.open(this.method,this.uri,false);
ajaxer.setRequestHeader("Method", "POST " + this.uri + " HTTP/1.1");
ajaxer.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
callback(ajaxer.responseBody);
}catch(e){}
}
this.process = function(){
// init request object
var ajaxer = getObjects();
if ( !this.enabled )
{
if (myonExceprion != null ) myonExceprion("ajax-debug:没有可供使用的对象");
return;
}
try
{
// bind the complete handler
var toncomplete = sonComplete;
if ( toncomplete == null && this.onComplete != null )
{
toncomplete = this.onComplete;
}
var thandler = this.shandler(ajaxer,toncomplete);
ajaxer.onreadystatechange = thandler;
// bind finally.start to send the data;
if( this.method == "POST" )
{
ajaxer.open(this.method,this.uri,true);
ajaxer.setRequestHeader("Method", "POST " + this.uri + " HTTP/1.1");
ajaxer.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajaxer.setRequestHeader("Accept","text/service");
ajaxer.send(this.data);
}
else
{
// to get
ajaxer.open(this.method,this.uri + "?" + this.data,true);
ajaxer.send(this.data);
}
}catch(e)
{
// exception raise
if ( this.method == "POST" )
{
try{
// try to get
ajaxer.open("GET",this.uri + "?" + this.data,true);
ajaxer.send(this.data);
}catch(ex){
//send process fail.
if ( myonExceprion != null ) myonExceprion("ajax-debug:发送尝试步骤全部失败,请求被终止");
}
}
else
{
if ( myonExceprion != null ) myonExceprion("ajax-debug:发送尝试步骤全部失败,请求被终止");
}
}
delete(ajaxer);
}
this.shandler = function( Ajaxer,completehandler )
{
return function(){
if ( Ajaxer.readyState == 4 )
{
if ( Ajaxer.status == 200 )
{
try
{
var xmldom = new class_xmldom(Ajaxer.responseXML,Ajaxer.responseText);
var node = xmldom.selectSingleNode("/result/code");
var messagenode = xmldom.selectSingleNode("/result/message");
if ( node != null && messagenode != null )
{
try
{
this.statusCode = parseInt(node.Text);
this.statusText = "ajax-debug:" + messagenode.Text;
}catch(e){}
}
completehandler(xmldom,this.statusCode,this.statusText);
} catch ( ex ) { if (myonExceprion != null ) myonExceprion("ajax-debug:" + ex); }
finally
{
if ( Ajaxer != null ) delete(Ajaxer);
}
}
else
{
if ( myonExceprion != null ) myonExceprion("ajax-debug:接口返回HTTP状态码 " + Ajaxer.status + ",任务终止");
}
}
}
}
// ------------ load ------------ //
if ( this.uri != null && this.uri.length > 0 ) this.process();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -