readyState 属性 存储XMLHttpRequest的状态,从0到4变化 0:请求为初始化 1:服务器连接已建立 2:请求已接收 3:请求处理中 4:请求已完成,且响应已就绪 status 200:“ok” 404:paga not found onreadystatechange 存储函数名,在每次readyState变化时调用存储的函数
一个基本的请求相映模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
functionloadXMLDoc(){ var xmlhttp = null; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") } xmlhttp.onreadystatechange = function(){ if(xmlhttp.state == 200 && xmlhttp.readeyState == 4){ //获取响应数据 var responseT = xmlhttp.responseText; } }; xmlhttp.open("GET","www.baidu.com",true); xmlhttp.send(null); }