JavaScript----Ajax

var xhr=new XMLHttpRequest();
xhr.open('get',url,true);//默认为真,则是同步模式,设置为false则为异步模式
xhr.send(null);//post请求必须设置主体内容,否则设置null
xhr.onreadystatechange=function(){
//每当 readyState 改变时,就会触发 onreadystatechange 事件,所以我们可以通过事件来捕获响应的状态,从而决定下一步。
     if(xhr.readyState==4 && xhr.status==200){
//这里的readyState和status是判断当前状态是否成功
    document.getElementById("dv").innerHTML=xhr.responseText;
//这里的responseText就是我们得到的响应主题内容,可以直接通过dom对象渲染到页面。
} 
};

  

猜你喜欢

转载自www.cnblogs.com/damon35868/p/10090824.html