Ajax异步封装

 1  //自己封装了一个异步方法。 
 2         //第一个参数:GET或者是POST,二个参数:请求的url地址,
 3         //第三个:是否异步第四个:往后台发送的Post的数据,最后一个后台返回数据之后,处理数据的回调函数。
 4         function myAjax(method,url,isAsync,postData,afterSuccess) {
 5             var xhr;
 6             
 7             if (XMLHttpRequest) {//ff,ie8,chrome
 8                 xhr = new XMLHttpRequest();
 9             } else {//兼容ie6,ie5
10                 xhr = new ActiveXObject("Microsoft.XMLHTTP");
11             }
12 
13             xhr.open(method, url, isAsync);
14 
15             
16             xhr.send(postData);//发送请求。
17 
18             xhr.onreadystatechange = function () {
19                 if (xhr.readyState == 4 && xhr.status == 200) {
20                     //alert(xhr.responseText);
21                     afterSuccess(xhr.responseText);
22                 }
23             };
24         }

 

posted @ 2017-04-09 17:03  liusheng11188  阅读(194)  评论(0编辑  收藏  举报