简单封装了下基本的AJAX实现

None.gif function ajaxReq()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    var url
=null;//服务器端的处理页面地址
InBlock.gif
    var reqResult=null//服务器端处理后的结果
InBlock.gif
    var rdstate=null//状态信息
InBlock.gif

InBlock.gif    
this.doSendResuest=function(_url)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    url
=_url;
InBlock.gif    var xmlHttp 
= false;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*@cc_on @*/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*@if (@_jscript_version >= 5)
InBlock.gif    try {
InBlock.gif      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
InBlock.gif    } catch (e) {
InBlock.gif      try {
InBlock.gif        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
InBlock.gif      } catch (e2) {
InBlock.gif        xmlHttp = false;
InBlock.gif      }
InBlock.gif    }
ExpandedSubBlockEnd.gif    @end @
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (!xmlHttp && typeof XMLHttpRequest != 'undefined'dot.gif{
InBlock.gif      xmlHttp 
= new XMLHttpRequest();
ExpandedSubBlockEnd.gif    }

InBlock.gif     
InBlock.gif    xmlHttp.open(
"post",url,true);
InBlock.gif    xmlHttp.send(
null);
InBlock.gif   
InBlock.gif    
InBlock.gif     
if (xmlHttp.readyState == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
InBlock.gif            rdstate
="正在读取信息dot.gif.\n";
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        xmlHttp.onreadystatechange 
= function() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
if(xmlHttp.readyState == 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                 
if(xmlHttp.status==200)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        rdstate
+="更新完成\n";
InBlock.gif                        reqResult
=xmlHttp.responseText;
InBlock.gif                       
ExpandedSubBlockEnd.gif                     }

ExpandedSubBlockEnd.gif                 }

ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
InBlock.gif    
InBlock.gif    
InBlock.gif    
InBlock.gif    
this.getState=function()    //获取状态信息
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return rdstate;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.getResult=function()     //获取返回结果TEXT
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
return reqResult;
ExpandedSubBlockEnd.gif    }

InBlock.gif   
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
None.giffunction ajaxProcess()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif     
InBlock.gif         
InBlock.gif    var e
=window.event.srcElement;
InBlock.gif    var selindex
=e.options[e.selectedIndex].value;
InBlock.gif    
InBlock.gif    var url
= window.location.href +"?value="+selindex;
InBlock.gif    
InBlock.gif    var obj
=new ajaxReq();
InBlock.gif    obj.doSendResuest(url);
InBlock.gif   var rdstate
=obj.getState();
InBlock.gif   alert(rdstate);
InBlock.gif   var reqResult
=obj.getResult();
InBlock.gif   alert(reqResult);
InBlock.gif    var rdstate
=obj.getState();
InBlock.gif   
InBlock.gif   doresult(reqResult,rdstate);
InBlock.gif    
InBlock.gif   
ExpandedBlockEnd.gif}

None.gif
这是最基本的封装,有时间再改进下.

转载于:https://www.cnblogs.com/super-yc/archive/2007/05/31/766602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值