var $ = {
????request:function(obj){
????????//1. 獲得xmlhttprequest對象兼容性處理
????????var xhr;????//undefined未定義
????????try{
????????????//主流瀏覽器里面的ajax對象
????????????xhr = new XMLHttpRequest();
????????}catch(e){
????????????//IE低版本的瀏覽器
????????????xhr = new ActiveXObject("Microsoft.XMLHTTP");
????????}
?
????????//2. 建立和服務器的連接
????????if(obj.method=='get'){
????????????xhr.open(obj.method,obj.url+'?'+obj.data+'&'+Math.random(),true);
????????????xhr.send();
????????}else if(obj.method=='post'){
????????????xhr.open(obj.method,obj.url,true);
????????????xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
????????????xhr.send(obj.data);
????????}
????????//監視服務器的處理狀態
????????xhr.onreadystatechange = function(){
????????????if(xhr.readyState==4 && xhr.status==200){
????????????????//說明請求成功了,輸出服務器返回的數據
????????????????obj.callback(xhr.responseText);
????????????}
????????}
????}
}
文章來源:劉俊濤的博客
地址:http://www.cnblogs.com/lovebing
歡迎關注,有問題一起學習歡迎留言、評論。