注意的問題:
1 在伺服器中運行測試
2 注意編碼的問題,編碼要統一,否則讀取的資訊會亂碼
HTML代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> New Document </title> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <script type="text/javascript">function doAjaxCall(the_request){var request=null;if(window.XMLHttpRequest){request=new XMLHttpRequest();}else if(window.ActiveXObject){request=new ActiveXObject("Microsoft.XMLHTTP");}if(request){request.open("GET",the_request,true);request.onreadystatechange=function(){if(request.readyState===4){if (request.status == 200 || request.status == 0){ document.getElementById("vv").innerHTML=request.responseText;}}}request.send(null);}else{alert("error");}} </script> </head> <body> <input type="button" value="make" onclick="doAjaxCall('ajax-1.txt');"/> <div id="vv"></div> </body></html>
TXT檔案內容:
this is the first test
ajax讀取常值內容原理較為簡單,在核心思想掌握之後,對大型的開發應用是很容易上手的,重點基礎是熟悉XMLHTTP對象的屬性和方法,對其應用得心應手才會寫出使用者感受良好的應用。