原代碼問題
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { ... }
else { alert('sb'); } // readyState 1/2/3 時也會彈 sb
};
修正邏輯
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) { // 僅請求完成
if (xmlhttp.status == 200) {
alert(this.responseText);
} else {
alert('HTTP錯誤: ' + xmlhttp.status + ' ' + xmlhttp.responseText);
}
}
};
服務端檢查
- 瀏覽器直接訪問
/MemberController.class.php/ajax 確認路由正確
- ThinkPHP 控制器方法需為
public function ajax(),路由配置支持
- 返回純文本/JSON,避免輸出 HTML 佈局