function ajax(data){
//data={data:"",dataType:"xml/json",type:"get/post"£¬url:"",asyn:"true/false",success:function(){},failure:function(){}}
//µÚÒ»²½£º´´½¨xhr¶ÔÏó
var xhr = null;
if(window.XMLHttpRequest){//±ê×¼µÄä¯ÀÀÆ÷
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//µÚ¶þ²½£º×¼±¸·¢ËÍÇ°µÄһЩÅäÖòÎÊý
var type = data.type == 'get'?'get':'post';
var url = '';
if(data.url){
url = data.url;
if(type == 'get'){
url += "?" + data.data + "&_t="+new Date().getTime();
}
}
var flag = data.asyn == 'true'?'true':'false';
xhr.open(type,url,flag);
//µÚÈý²½£ºÖ´Ðз¢Ë͵Ķ¯×÷
if(type == 'get'){
xhr.send(null);
}else if(type == 'post'){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(data.data);
}
//µÚËIJ½£ºÖ¸¶¨»Øµ÷º¯Êý
xhr.onreadystatechange = function(){
if(this.readyState == 4){
if(this.status == 200){
if(typeof data.success == 'function'){
var d = data.dataType == 'xml'?xhr.responseXML:xhr.responseText;
data.success(d);
}
}else{
if(typeof data.failure == 'function'){
data.failure();
}
}
}
}
}
¡¡