<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> </head> <body> <input type="text" id="txt1" value="1212" /> <script> var obj = document.getElementById('txt1'); console.log(obj.value); var xmlHttp; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { console.log(1); obj.value = xmlHttp.responseText; } }; xmlHttp.open('get', 'json1.json', true); // true 异步 false 同步 url: aaa.asp?a=1&b=2 xmlHttp.send(); //send('11111'). 当post的时候提交 '11111' console.log(2); //xmlhttp.open("POST", "ajax_test.asp", true); //xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //xmlhttp.send("fname=Bill&lname=Gates"); //当为true,打出顺序为 2 1 //当为false,打出顺序为 1 2 </script> </body> </html>
教程地址:http://www.w3school.com.cn/ajax/index.asp