js-ajax-01
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="button" value="请求" id="btn"> <script type="text/javascript"> var oBtn=document.getElementById('btn'); oBtn.onclick=function(){ //1 创建一个请求 if (window.XMLHttpRequest) { //chrome safari forefox ie7+ var httpRequest = new XMLHttpRequest(); } else { //IE6 var httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } //2 连接服务器 // httpRequest.open(发送方式(get/post),url请求的地址,true异步-默认/false-同步) httpRequest.open('GET', 'a.txt'); //3 发送请求 httpRequest.send(); //4 接受结果 httpRequest.onreadystatechange = function() { if (httpRequest.readyState == 4) { if (httpRequest.status === 200) { //请求过来的东西,放到 re...Text console.log(httpRequest.responseText); //把input的value变成我请求的文字 oBtn.value = httpRequest.responseText; //已经完成 } else { //有一个有问题的请求 // 404-页面没有找到 或 500 服务器错误 } } } } </script> </body> </html>