`
sunxboy
  • 浏览: 2830509 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

项目积累--ajax.js封装

阅读更多
js 代码
 
  1. //////////////////ajax封装  
  2.   
  3. var req;   
  4. var xmlDoc,xmlHttpRequest;  
  5. var ajaxExcetpion;  
  6. ///////////调用的URL(以GET方式组装的参数),回调方法callback  
  7. ///////////返回:成功返回true,失败返回false;  
  8. function ajaxCallByGet(url,callback,win_self)  
  9. {  
  10. if (window.XMLHttpRequest) {  
  11.                 win_self.req = new XMLHttpRequest();  
  12.                     if (req.overrideMimeType){  
  13.                     req.overrideMimeType('text/xml');  
  14.                         }     
  15.          } else if (window.ActiveXObject) {  
  16.             if (xmlHttpRequest) {  
  17.               win_self.req= new ActiveXObject(xmlHttpRequest);  
  18.             }else {  
  19.                    var versions = ["Msxml2.XMLHTTP.7.0""Msxml2.XMLHTTP.6.0""Msxml2.XMLHTTP.5.0""Msxml2.XMLHTTP.4.0""MSXML2.XMLHTTP.3.0""MSXML2.XMLHTTP","MSXML.XMLHTTP",  
  20.                         "Microsoft.XMLHTTP"];  
  21.                for (var i = 0; i < versions.length ; i++) {  
  22.                  try {  
  23.                      win_self.req = new ActiveXObject(versions[i]);  
  24.                      if (win_self.req) {  
  25.                         xmlHttpRequest = versions[i];  
  26.                         ajaxExcetpion = null;  
  27.                         break;  
  28.                     }  
  29.                 }  
  30.                 catch (objException) {  
  31.                     ajaxExcetpion = objException;  
  32.                 } ;  
  33.             }  
  34.             ;  
  35.         }  
  36.     }  
  37.       
  38.     if(ajaxExcetpion){  
  39.       return false;       
  40.     }  
  41.     if(req){   
  42.                       
  43.          req.open("GET", url, true);   
  44.          req.onreadystatechange = callback;   
  45.          req.send(null);   
  46.     }  
  47.      return true;  
  48. }  
  49.   
  50.   
  51. /* 
  52.  * Returns an new XMLHttpRequest object, or false if the browser 
  53.  * doesn't support it 
  54.  */  
  55. function newXMLHttpRequest() {  
  56.   
  57.     var xmlreq = false;  
  58.       
  59.     if (window.XMLHttpRequest) {  
  60.         xmlreq = new XMLHttpRequest();  
  61.         if (xmlreq.overrideMimeType){  
  62.             xmlreq.overrideMimeType('text/xml');  
  63.         }     
  64.     } else if (window.ActiveXObject) {  
  65.         if (xmlHttpRequest) {  
  66.             xmlreq= new ActiveXObject(xmlHttpRequest);  
  67.         }else {  
  68.             var versions = ["MSXML.XMLHTTP",  
  69.                         "Microsoft.XMLHTTP","Msxml2.XMLHTTP.7.0""Msxml2.XMLHTTP.6.0""Msxml2.XMLHTTP.5.0""Msxml2.XMLHTTP.4.0""MSXML2.XMLHTTP.3.0""MSXML2.XMLHTTP"];  
  70.             for (var i = 0; i < versions.length ; i++) {  
  71.                 try {  
  72.                     xmlreq = new ActiveXObject(versions[i]);  
  73.                     if (xmlreq) {  
  74.                         xmlHttpRequest = versions[i];  
  75.                         ajaxExcetpion = null;  
  76.                         break;  
  77.                     }  
  78.                 }  
  79.                 catch (objException) {  
  80.                     ajaxExcetpion = objException;  
  81.                 }   
  82.                   
  83.             }  
  84.               
  85.         }  
  86.     }  
  87.   
  88. return xmlreq;  
  89. }  
  90.   
  91.   
  92.     /* 
  93.     * Returns a function that waits for the specified XMLHttpRequest 
  94.     * to complete, then passes it XML response to the given handler function. 
  95.   * req - The XMLHttpRequest whose state is changing 
  96.   * responseXmlHandler - Function to pass the XML response to 
  97.   */  
  98.  function getReadyStateHandler(req, responseXmlHandler) {  
  99.   
  100.    // Return an anonymous function that listens to the XMLHttpRequest instance  
  101.    return function () {  
  102.   
  103.      // If the request's status is "complete"  
  104.      if (req.readyState == 4) {  
  105.          
  106.        // Check that we received a successful response from the server  
  107.        if (req.status == 200) {  
  108.   
  109.          // Pass the XML payload of the response to the handler function.  
  110.          responseXmlHandler(req);  
  111.   
  112.        } else {  
  113.   
  114.          // An HTTP problem has occurred  
  115.          alert("HTTP error "+req.status+": "+req.statusText);  
  116.        }  
  117.      }  
  118.    }  
  119.  }  
  120.   
  121.   
  122. function test()  
  123. {   
  124.   alert("test");  
  125. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics