用下面我失敗的程式碼當範例
function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) { var response = xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); //Send the proper header information along with the request xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(); }
看起來很正常的程式碼,還是從w3cschool上面幾乎一樣的東西,但就是有錯誤。
只要把條件式稍微改一下
if( xmlhttp.readyState == 4 ) { if( xmlhttp.status == 200 ) { ...... } }
收工。
真是整人Orz