Ajax通用类

来源:互联网 发布:淘宝网电脑客户端 编辑:程序博客网 时间:2024/05/22 14:51
        在用Ajax时,一般要在每个页面添加XMLHttpRequest定义与发送请求的JavaScipt代码,使页面代码增长。本文特将页面中重复的Ajax代码封装到一个JS文件中,在需要的页面引用它就实现Ajax功能。
AjaxHelper.js文件:
  1. //Ajax类定义
  2. function AjaxHelper()
  3. {
  4.     //构造函数
  5.     var httpRequest = null;
  6.     var requestResult = "";
  7.     var sync = false;
  8.     
  9.     this.init();
  10. }
  11. //同步请求,默认为false
  12. AjaxHelper.prototype.sync=false;
  13. //初始化
  14. AjaxHelper.prototype.init = function()
  15. {
  16.     try 
  17.     {
  18.         httpRequest = new ActiveXObject("Msxml2.httpRequest");
  19.     } 
  20.     catch (e1) 
  21.     {
  22.         try 
  23.         {
  24.             httpRequest = new ActiveXObject("Microsoft.httpRequest");
  25.         } 
  26.         catch (e2) 
  27.         {
  28.             try 
  29.             {
  30.                 httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  31.             } 
  32.             catch (e3) 
  33.             {
  34.                 httpRequest = null;
  35.             }
  36.         }
  37.     }
  38.     
  39.     if(httpRequest == null)
  40.     {
  41.         alert("Error initializing AjaxHelper! Can't create XMLHttpRequest object.");
  42.     }
  43. }
  44. //发送请求
  45. AjaxHelper.prototype.request = function(url, postData)
  46. {
  47.     var isPost = arguments.length > 1;
  48.     var method = isPost ? "POST" : "GET";
  49.     
  50.     httpRequest.open(method, url, this.sync);
  51.     httpRequest.onreadystatechange = createFunction(this"handleResponse"this.requestCallback);
  52.     if(isPost)
  53.         httpRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
  54.         httpRequest.send(postData);
  55. }
  56. //处理请求结果
  57. AjaxHelper.prototype.handleResponse = function(callbackFunc)
  58. {
  59.     if (httpRequest.readyState == 4)
  60.     {
  61.         if (httpRequest.status == 200) //正常
  62.         {
  63.             callbackFunc(httpRequest.responseText);
  64.         }
  65.         else if (httpRequest.status == 404)
  66.         {
  67.             alert("Request URL does not exist.");
  68.         }
  69.         else if (httpRequest.status == 403) 
  70.         {
  71.             alert("Access denied.");
  72.         }
  73.         else
  74.         {
  75.             alert("Error: status code is " + httpRequest.status + ".");
  76.         }
  77.     }
  78. }
  79. //处理请求结果事件,需要客户端定义
  80. AjaxHelper.prototype.requestCallback = function(result){}
  81. //将有参数的函数封装为无参数的函数
  82. function createFunction(obj, funcName)
  83. {
  84.     var args = [];
  85.     if(!obj)
  86.         obj = window;
  87.     for(var i=2; i<arguments.length; i++)
  88.         args.push(arguments[i]);
  89.         
  90.     return function()
  91.     {
  92.         obj[funcName].apply(obj, args);
  93.     }
  94. }

其中最后一个createFunction函数是将有参数的函数封装为无参数的函数,使客户可以用事件方式来处理请求结果。

 

使用方式:

1、在页面中添加JS引用

  1. <script type="text/jscript" language="javascript" src="AjaxHelper.js"></script>

 

2、定义请求结果处理函数

  1. function handleResult(result)
  2. {
  3.     //处理代码
  4. }

3、使用Ajax请求

  1. function requestData()
  2. {
  3.     var ajax = new AjaxHelper();
  4.     ajax.requestCallback = handleResult;
  5.     ajax.request("请求页面URL");//GET
  6.     //ajax.request("请求页面URL", postData);//POST
  7. }
原创粉丝点击