简单的ajax封装

jerry thinkphp 2015年11月18日 收藏
简单的原生javascript ajax情趣方法
我怎么总是上床不了附件。。。
是不是有时候只想简单的利用一下ajax功能,而不像引入jquery呢,这时候这个东西可以帮你
这个是简单版本 没有加入时间超时限制,用法和jquery的get post 相似

供大家参考
  1. /**
  2.  * 简单的ajax封装方法 默认返回 html
  3.  * @Author yulipu
  4.  * @version 1.0
  5.  *
  6.  * @param receiveType 返回数据类型
  7.  *        参数取值 html xml json
  8.  * 用法
  9.  *        function demo() {
  10.  *            var xmlhttp = new simpleYAjax('html');  //不填参数默认就是html
  11.  *            xmlhttp.post('ajax.php', 'name=张三', function(s) {
  12.  *                document.getElementById('conDiv').innerHTML = s;
  13.  *            });
  14.  *            
  15.  *            var xmlhttp = new simpleYAjax('json');  //不填参数默认就是html
  16.  *            xmlhttp.get('ajax.php?param=1', function(json) {
  17.  *                alert(json.xxx);
  18.  *            });
  19.  *        }
  20.  * 
  21.  */
  22. function simpleYAjax(receiveType) {
  23.     var self = this;
  24.     
  25.     //一个变量定义了但没赋值则值为undefined
  26.     //typeof 后面若是一个没有定义过的变量则值为 undefined 若是定义过但没赋值的变量 值也是undefined
  27.     this.receiveType = (undefined == receiveType ? 'html' : receiveType);
  28.     this.callBack = null;  //回调函数
  29.     this.xmlhttp = this.init();  //xmlHttpRequest对象
  30.     
  31.     //把服务器返回数据传给回调函数
  32.     this.processCallBack = function() {
  33.         if(self.xmlhttp.readyState == 4) {
  34.             if(200 == self.xmlhttp.status || 0 == self.xmlhttp.status) {  // 0是没经过服务器
  35.                 if(self.receiveType == 'html') {
  36.                     self.callBack(self.xmlhttp.responseText);  //调用回调函数
  37.                     
  38.                 } else if(self.receiveType == 'xml') {
  39.                     self.callBack(self.xmlhttp.responseXML);
  40.                     
  41.                 } else if(self.receiveType == 'json') {
  42.                     try {
  43.                         self.callBack(JSON.parse(self.xmlhttp.responseText));
  44.                     } catch(e) { 
  45.                         var str = '(' + self.xmlhttp.responseText + ')';  //json字符串
  46.                         self.callBack(eval(str));
  47.                     }
  48.                     
  49.                 } else {
  50.                     //others
  51.                 }
  52.             }
  53.         }
  54.     };
  55.     
  56. }

  57. /**
  58.  * 初始化xmlHttpRequest对象
  59.  */
  60. simpleYajax.prototype.init = function() {
  61.     var xmlhttp = null;
  62.     
  63.     //需要针对不同轮浏览器建立这个对象的不同方式写不同代码
  64.     if (window.XMLHttpRequest) {
  65.         //针对FireFox、Mozillar、Opera、Safari、IE7、IE8
  66.         xmlhttp = new XMLHttpRequest();
  67.         //针对某些特定版本的Mozillar浏览器的BUG进行修正
  68.         if (xmlhttp.overrideMimeType) {
  69.             xmlhttp.overrideMimeType("text/xml");
  70.         }
  71.     } else if (window.ActiveXObject) {
  72.         //针对IE6、IE5.5、IE5
  73.         //IE中创建XMLHttpRequest对象要用到对象的控件名,例如:
  74.         //var xmlhttp = new ActiveXObject("控件名");
  75.         //可用的控件名有:MSXML2.XMLHTTP, Microsoft.XMLHTTP
  76.         //我们用一个for循环来让他自己判断用哪个控件名可以创建出XMLHttpRequest对象

  77.         var activexName = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
  78.         for (var i=0; i<activexName.length; i++) {
  79.             try {
  80.                 //取出一个控件名进行创建,如果成功就结束循环
  81.                 //如果创建失败就抛出异常,不做处理,让他继循环尝试创建
  82.                 xmlhttp = new ActiveXObject(activexName[i]);
  83.                 break;
  84.             } catch(e) {}
  85.         }
  86.     }
  87.     
  88.     return xmlhttp;
  89. };

  90. /**
  91.  * post方式发送请求
  92.  * @param targetUrl 服务端地址
  93.  * @data 发送的数据
  94.  * @callBack 回调函数 回调函数接收一个参数,就是服务器端返回的结果
  95.  */
  96. simpleYajax.prototype.post = function(targetUrl, data, callBack) {
  97.     if(callBack) {  //typeof 后面是函数 则 结果是'function'
  98.         this.callBack = callBack;
  99.         this.xmlhttp.onreadystatechange = this.processCallBack;
  100.         this.xmlhttp.open('POST', targetUrl, true);  //打开与服务器连接
  101.         this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  //post方式要设置请求类型
  102.         this.xmlhttp.send(data);  //发送内容到服务器
  103.     }
  104. };

  105. /**
  106.  * get方式发送请求
  107.  * @param targetUrl 服务端地址
  108.  * @callBack 回调函数 回调函数接收一个参数,就是服务器端返回的结果
  109.  */
  110. simpleYajax.prototype.get = function(targetUrl, callBack) {
  111.     if(callBack) {
  112.         this.callBack = callBack;
  113.         this.xmlhttp.onreadystatechange = this.processCallBack;  //注册回调函数 processCallBack把内容处理后回传给callBack
  114.         
  115.         //if(window.XMLHttpRequest) {
  116.             this.xmlhttp.open('GET', targetUrl);
  117.             this.xmlhttp.send(null);
  118.         //} else {
  119.         //    this.xmlhttp.open('GET', targetUrl, true);
  120.         //    this.xmlhttp.send();
  121.         //}
  122.     }
  123. };