1. 程式人生 > >ajax基本原理實現

ajax基本原理實現

ont () header fun ocr head activex div var

function ajax(method,url,data,success){

try{
var xhr=new XMLHttpRequest();
}catch(e){
xhr=new ActiveXObject(‘Mocrosoft.XMLHTTP‘);
}
if(method==‘get‘&&data){
url+=‘?‘+data;
}
xhr.open(method,url,true);
if(method==‘get‘){
xhr.send();
}else{
xhr.setRequestHeader(‘Content-type‘, ‘application/x-www-form-urlencoded‘);
xhr.send(data);
}
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
success&&success(xhr.responseText);
//console.log(xhr.responseText);
}
}
}

ajax基本原理實現