1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <!DOCTYPE html> <html lang="ch"> <head> <meta charset="UTF-8"> <title>get请求</title> <script> window.onload=function (){ document.getElementById("helloajax").onclick=function (){ var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function (){ if (this.readyState == 4) { if(this.status==404){ alert("你访问的资源不存在"); }else if(this.status==500){ alert("服务器内部发生错误,请联系管理员"); }else if(this.status==200){ console.log(this.responseText); document.getElementById("mydiv").innerHTML=this.responseText; }else { alert("出现了其他错误") }
} } xhr.open("get","/old/ajaxrequest1",true); xhr.send(); } } </script> </head> <body> <input type="button" value="hello ajax" id="helloajax"> <div id="mydiv"></div> </body> </html>
|