例子描述:点击按钮,通过传统的javascr 的Ajax方式获取从服务器端取回文件信息并显示。
程序如下:
<body> <input type="button" value="ajax submit" οnclick="Ajax();"> <div id="resText"></div> <script type="text/javascript" src="jQuery1.11.1/jquery-1.3.1.js"></script> <script type="text/javascript"> function Ajax() { var xmlHttpReq = null; if(window.ActiveXObject){ xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP'); }else if(window.XMLHttpRequest){ xmlHttpReq = new XMLHttpRequest(); } xmlHttpReq.open('GET','serve/test.php',true); 此处为服务器端脚本文件的地址 xmlHttpReq.onreadystatechange = RequestCallBack; xmlHttpReq.send(); function RequestCallBack() { if(xmlHttpReq.readyState ===4){ if(xmlHttpReq.status === 200){ document.getElementById('resText').innerHTML = xmlHttpReq.responseText; } } } } </script> </body
点击按钮后,结果图如下: