第一個Ajax調用:
本次嘗試首先是在php+MySQL+Apache環境下,通過javascript實現Ajax例子。以下是代碼核心:function Ajax(){var xmlHttPReq= null;if(window.ActiveXObject){xmlhttpReq =new ActiveXObject("Microsoft.XMLHTTP");}else if(window.XMLHttpRequest){xmlHttpReq =new XMLHttpRequest();}if(xmlHttpReq != null){xmlHttpReq.open("GET","test.php",true);xmlHttpReq.onreadystatechange= RequestCallBack;xmlHttpReq.send(null); }function RequestCallBack(){if(xmlHttpReq.readyState == 4){if(xmlHttpReq.status == 200){document.getElementById("resText").innerHTML =xmlHttpReq.responseText; } } }}html代碼
以下是html的完整代碼
<!DOCTYPE HTML><html><head><meta charset="utf-8"><title>Ajax測試頁</title><script type="text/javascript"></script><style type="text/CSS"></style></head><body> <input type="button" value="Ajax提交" onclick="Ajax();" /> <div id="resText"></div></body></html>PHP代碼
php代碼較為簡單,保存為text.php
<?phpecho "hello Ajax!";?>代碼粘貼的一個小問題:如何在markdown格式下粘貼完整的html代碼? 目前我摸索的解決方式:使用轉義符。
新聞熱點
疑難解答