/**********************************************
* @author skyz
* @function javascript client ajax dealwith
* @datetime 2006-3-20
**********************************************
* function: create a httprequest object
**********************************************/
function httprequest(){
this._httprequest=null; //httprequest request object
this._callback=null; //call back function
this._domresult=true; //result if dom object or text string
this._requestdata=null; //request data
this._requestresult=null; //httprequest result
this._statestring=null; //current request state string
this._error=false; //current if have error
this._callbackpara=null; //current callback function parama
//internal method for get httprequestobject
this.init=function(){
//judge if not ie
if(window.xmlhttprequest){
this._httprequest=new xmlhttprequest();
//set request mime is text/xml
if(this._httprequest.overridemimetype){
this._httprequest.overridemimetype('text/xml');
}
}else if(window.activexobject){
try{
this._httprequest=new activexobject("msxml2.xmlhttp");
}catch(ex){
try{
this._httprequest=new activexobject("microsoft.xmlhttp");
}catch(ex){
this._setmessage(ex,true);
return;
}
}
// this._httprequest.setrequestheader("content-type","text/xml;charset=gb2312");
}
//judge httprequest object create successful
if(!this._httprequest){
this._setmessage("xmlhttprequest 对象创建失败!请重试......",true);
return;
}
}
/*
* function: set the request header
* namepar:request's header name
* valuepar:request's header value
*/
this.dosetrequestheader=function(namepar,valuepar){
if(this._error){
return;
}
this._httprequest.setrequestheader(namepar,valuepar);
}
/*
* function: set the request data
* datapar:request's send data;
*/
this.dosetrequestdata=function(datapar){
if(this._error){
return;
}
this._requestdata=datapar;
}
/*
*function get requesthttp object
*/
this._getrequestobj=function(){
if(this._error){
return;
}
return this._httprequest;
}
/*
* function:set callback function para
*/
this.dosetcallback=function(callback,paradata){
this._callback=(callback)?callback:null;
this._callbackpara=(paradata)?paradata:null;
};
/*
* function: get current statestring
*/
this.dogetstate=function(){
return this._statestring;
}
/*
* function: get current error
*/
this.dogeterror=function(){
return this._error;
}
/*
*
*/
this.docallback=function(){
this._callback(this._requestresult,this._callbackpara);
}
/*
* function: send the request
* urlpar: request's url path
* [methodpar]:request's method
* [dompar]: request's result is dom or string
*/
this.dosendresuest=function(urlpar,methodpar,obj,dompar,asypar){
if(obj._error){
return;
}
methodpar=((methodpar)?methodpar:"get");
asypar=((asypar)?asypar:true);
this._domresult=(dompar)?dompar:obj._domresult;
try{
var a=this._getrequestobj();
a.onreadystatechange=function(){
if(obj._error){
return;
}
var readystatetmp=a.readystate;
if(readystatetmp==0){
obj._setmessage("未初始化!");
}else if(readystatetmp==1){
obj._setmessage("正在读取中......");
}else if(readystatetmp==2){
obj._setmessage("已经读取过!");
}else if(readystatetmp==3){
obj._setmessage("正在逐个切换......");
}else if(readystatetmp==4){
var statustmp=a.status;
if(statustmp==404){
obj._setmessage("未找到请求页面!",true);
}else if(window.location.href.indexof("http")==-1 || statustmp==200){
obj._setmessage("完成!");
if(this._domresult && window.xmlhttprequest){
obj._requestresult=a.responsexml;
}else{
obj._requestresult=a.responsetext;
}
if(obj._callback){
obj.docallback();
}
}else{
obj._setmessage("未知错误!");
}
}else{
obj._setmessage("未知错误!");
}
}
a.open(methodpar,urlpar,asypar);
// a.setrequestheader("if-modified-since","0");
a.send(obj._requestdata);
}catch(ex){
obj._setmessage(ex,true);
}
}
/*
* function: deal exception error
* expar:error string
*/
this._setmessage=function(expar,mark){
this._statestring=expar.tostring();
this._error=(mark)?mark:false;
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
7月15日笔试总结
测验项目:3C数码商城+简答题+选择题测验时间:3小时不足之处:简答题3道题留白,完全不懂,仍需复习,一道题简写...
[阅读全文]
-
-
-
-
-
原生AJAX
AJAXAJAX 是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部...
[阅读全文]
-
-
网友评论