使用AJAX的返回值跟字符串進行比較,無論返回的結果是否與字符串相同都是false問題出在var txt = new String(xhr.responseText); //獲得一個 xml dom 對象。 if("no"==txt1){ alert("no"); }else{ alert("yes"); } //dom 操作、更新頁面 }下面是具體的代碼function query(){ var xhr = getXmlHttpRequest(); xhr.open('post','servlet/RegistAction',true); //必須添加一個消息頭 content-type? xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.onreadystatechange=function(){ //編寫相應的處理代碼 if(xhr.readyState == 4){ //只有 readyState 等亍 4,xhr 才完整地接收到了服務器返回的數據。 //獲得文本數據 var txt = new String(xhr.responseText); //獲得一個 xml dom 對象。 if("no"==txt1){ alert("no"); }else{ alert("yes"); } //dom 操作、更新頁面 } }; xhr.send('username='+document.getElementById("username").value); }
添加回答
舉報
0/150
提交
取消