用ajax技術實現
代碼: t.htm
| <script> function f(){ var req =new ActiveXObject("Microsoft.XMLHTTP"); req.open("GET","t.asp?t1="+t1.value, true); req.onreadystatechange = function(){ if (req.readyState == 4) { if (req.status == 200) { msg.innerText = req.responseXML.documentElement.tagName; } } } req.send(null); } </script> <div>1:顯示"OK"; 其他字符:顯示"Error"</div> <input id=t1 value=1> <input type=button value="檢測" onclick="javascript:f()"> <div id=msg></div> |
代碼:
| t.asp <% Response.ContentType="text/xml" if request.querystring("t1")="1" then response.write("<OK/>") else response.write("<ERROR/>") end if %> |
傳統的asp無刷新技術
代碼t.htm
| <script> function f(){ document.getElementById("o").src="t.asp?t1="+t1.value } </script> <div>1:顯示"OK"; 其他字符:顯示"Error"</div> <input id=t1 name=1 value=1> <input type=button value="檢測" onclick="javascript:f()"> <div id=msg></div> <iframe src="t.asp" style="display:none" id=o></iframe> |
代碼t.asp
| <% if request.querystring="" then response.end ss="ERROR" if request.querystring("t1")="1" then ss="OK" %> <script>parent.msg.innerText="<%=ss%>"</script> |
兩者都是無刷新提取服務器(數據庫)數據并實時顯示在客戶段,那為什么現在都要用 ajax呢,還要考慮中文編碼等麻煩問題。ajax到底好在哪里,請高手詳細說說,我可是一知半解,還沒有領會到 ajax的妙處。
在這里,我們暫時只討論 ajax 調用同域下的asp文件(也就是說自己寫的asp程序),小偷程序,偷取新聞,天氣預報等應用技術暫時不討論,例子寫得很簡,沒有進行錯誤處理等,僅為討論之用。
