实用 Ajax 工具函数

/**
*  FILE : HhlAjax.js
*  VERSION : V2.0
*  DESCRIPTION:
*    this function return an ajax object : var ajx=getAjaxInstance("http://hhl_program.download.csdn.net/");
*  PROPERTY:
*    xmlhttp -- for saving a Object of XMLHttpRequest
*    method -- "POST" or "GET"
*    failed -- default value is false,if value is true that you get no ajax instance
*    URL -- the url you want to access
*    params -- parameters container
*    response -- xmlhttp.responseText is the text value of the response
*    responseXML -- xmlhttp.responseXML is the XML value of the response
*
*
*
*/
function getAjaxInstance(urlStr){
 var __ajax=new Object();
 __ajax.xmlhttp = null;
 __ajax.method = "POST";
 __ajax.URL = urlStr || null;
 __ajax.failed = false;
 __ajax.params = [];
 __ajax.response = null;
 __ajax.responseXML = null;
 
       
 __ajax.add=function(name,value){
  var param=new Object();
  param.name=name;
  param.value=value;
  __ajax.params.push(param);
 };
 
 __ajax.callback = function(){
  alert(" callback function /n You can Override like /n yourAjaxInstance.callback=function(){/*...*/};");
 };
 
 __ajax.clear = function(){
  __ajax.params = [];
 };
 
 __ajax.create = function(){
  try{
   __ajax.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }catch (e1){
   try{
    __ajax.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }catch (e2){
    __ajax.xmlhttp = null;
   }
  }
  if (! __ajax.xmlhttp){
   if (typeof XMLHttpRequest != "undefined"){
    __ajax.xmlhttp = new XMLHttpRequest();
   }
   else{
    __ajax.failed = true;
   }
  }
 };
 
 __ajax.onerror = function(){
  alert(" Ajax Error : /n"+" status : "+__ajax.xmlhttp.status+"/n Error Text : "+__ajax.xmlhttp.statusText);
 };
 
 __ajax.seturl = function(urlString){
  __ajax.URL = urlString || null;
 };
 
 __ajax.submit = function(){
  if(__ajax.failed){
   alert("Creat XMLHttpRequest failed!");
  }
  else if( ! __ajax.URL){
   alert("Need the '__ajax.URL',which tell ajax instance to go where");
  }
  else{
   //use param before clear
   var queryString="?";
   for(var i=0;i<__ajax.params.length;i++){
    var param=__ajax.params[i];
    queryString = queryString+param.name+"="+param.value+"&";
   }
   queryString = queryString.substring(0,queryString.length-1);
   __ajax.xmlhttp.open(__ajax.method, __ajax.URL+queryString, true);
   queryString = null;

   __ajax.clear();
   try{
    __ajax.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
   }catch(e){}
      
   __ajax.xmlhttp.onreadystatechange = function(){
    if(__ajax.xmlhttp.readyState==4){
     __ajax.response = __ajax.xmlhttp.responseText;
     __ajax.responseXML = __ajax.xmlhttp.responseXML;
     
     if(__ajax.xmlhttp.status != 200){
      __ajax.onerror();
     }
     else{
      __ajax.callback();
     }
         }//end if(__ajax.xmlhttp.readyState==4){}
   };
   __ajax.xmlhttp.send();
  }//end if(__ajax.failed){}else{}
 };
 __ajax.create();
 return __ajax;
}// end function getAjaxInstance(){}

 

 

/**    HhlAjax_test.htm    *******/

 

 

<HTML>
 <HEAD>
  <SCRIPT type="text/javascript" SRC="HhlAjax.js"></SCRIPT>
  <SCRIPT type="text/javascript">
   function testAjax(){
    ajx=getAjaxInstance("http://reg.163.com/login.jsp");
    ajx.add("username","xxxx");
    ajx.add("password","*****");
    ajx.callback=function(){
     document.write(ajx.response);
    };
    ajx.submit();
    
    alert("continu access the same url 2nd");
    ajx.add("username","vvvvv");
    ajx.add("password","*****");
    ajx.callback=function(){
     document.write(ajx.response);
    };
    ajx.submit();
    
    alert("continu access a different url");
    ajx.seturl("http://www.google.com.hk/search");
    ajx.add("q","ajax+responsexml");
    ajx.method="GET";
    ajx.callback=function(){
     document.write(ajx.response);
    };
    ajx.submit();
   }
   testAjax();
   
  </SCRIPT>
 </HEAD>
 <BODY>
  
 </BODY>
</HTML>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值