編輯:我修復了已在答案中提到的PHP的錯誤,但我仍然有同樣的問題。
我試圖顯示錯誤消息,如果我的POST請求失敗,並顯示成功消息,如果它不失敗。它目前正確地顯示成功消息,但是如果我使sql失敗,即使在控制檯中也沒有任何反應。
JQUERY://我已刪除了不必要的代碼
$("#form").validate({
ignore:'',
errorPlacement: function(error, element) {
error.insertAfter(element.parent()); // <- make sure the error message appears after parent element (after textbox!)
},
rules: {
},
messages: {
},
submitHandler: function(form) {
var datastring = $("#form").serialize();
$.ajax({
type: "POST",
url: "myscript.php",
data: datastring,
dataType:"json",
success: function (response) {
if(response.status === "success") {
window.alert(response.comment);
} else if(response.status === "error") {
window.alert(response.comment);
}
}
}); // close $.ajax
} // close submit handler
}); //end validate
PHP:
if($row = sasql_fetch_array($insert))
{
//success message
$arr = array ('status'=>'success','comment'=> $custid);
echo json_encode($arr);
} else {
//error message
$arr = array ('status'=>'error','comment'=>'There has been an error. Please try again.');
echo json_encode($arr);
}
有沒有人有什麼問題可以在這裏的任何想法嗎?就像我說的,如果我在success
函數之後加上console.log(JSON.stringify(response));
,那麼在錯誤數組上就沒有任何東西返回。謝謝
是來自服務器的響應,當你做一個Ajax調用? – 2014-09-01 16:27:51
是的 - 爲了成功,如果sql失敗,雖然我沒有得到任何迴應。 – Janey 2014-09-01 16:29:33
你使用'+'來進行字符串連接嗎?它應該是'.'(這是'請再試一次'。+ $ custid') – Stryner 2014-09-01 16:30:45