//创建xmlhttprequest对象 var xmlhttp; /* 检查浏览器是否支持 xmlhttprequest 对象。如果支持,则创建 xmlhttprequest 对象。如果不支持,则创建 activexobject,主要针对ie5和ie6 */ if (window.xmlhttprequest) {// code for ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code for ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } //open(method,url,async) /* method指请求的类型:get 或 post url指请求的路径 async:true为异步,false为同步 */ //发送get请求 xmlhttp.open("get","aj?method=findlist",true); xmlhttp.send(); //发送post请求 xmlhttp.open("post","aj?method=findlist",true); xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded"); xmlhttp.send("name=xiaowang&age=21"); //获得服务器的响应信息 // console.log(xmlhttp.responsetext); -->因为是异步请求,所以此时服务器相应信息可能还未到,故不能获得 //判断请求是否完成以及是否有响应 xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200)//请求已完成,且响应就绪 { console.log(xmlhttp.responsetext); //将响应信息显示在控制台 } }
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论