0
在下面的代碼中,我在Firebug中得到'TypeError:response is null'。我嘗試過很多組合和嘗試獲得迴應的方法,但沒有成功。請看看並告訴我我做錯了什麼。謝謝!用jQuery Ajax提交表單並處理響應
$("a.logmein").colorbox({ width:"540", height:"420", href:function(){ return this.href + " #login"; }, onComplete: function(){
$("#formLoginUser").submit(function(){
$.ajax({
data: $(this).serialize(),
type: "POST",
url: $(this).attr('action'),
success: function(response) {
if(response.status == 'fail'){
$("#notifications").removeClass().addClass('ajaxerror').html(response.message).show();
} else {
$("#notifications").removeClass().addClass('ajaxsuccess').html(response.message).show();
}
console.log(response);
},
error: function (xhr, textStatus, errorThrown) {
$("#notifications").addClass('ajaxsuccess').html(xhr.responseText).show();
}
});
return false;
});
}});
ajax.php:
header("Content-Type:application/json");
switch($_REQUEST['action']){
case('formLoginUser'): $afm = $_POST['afm'];
$password = $_POST['password'];
$query_finduser = sprintf("SELECT * FROM clients WHERE clientafm=%s and clientcode=%s and expires>%s",
GetSQLValueString($afm, "text"),
GetSQLValueString($password, "int"),
GetSQLValueString(strtotime('now'), "int"));
$finduser = mysql_query($query_finduser, $connection) or die(mysql_error());
$row_finduser = mysql_fetch_assoc($finduser);
$totalRows_finduser = mysql_num_rows($finduser);
if($totalRows_finduser==0) {
echo json_encode(array("status"=>"fail", "message"=>"Login failed. Please retry"));
} else {
echo json_encode(array("status"=>"success", "message"=>"Login successful!"));
}
break;
default: break;
}//switch end
1.您確定'action'屬性設置正確嗎? (即它是否指向正確的腳本)。 2.你確定腳本正在輸出任何東西嗎? (哪個函數回調?它是否「成功」?)在這種情況下,你只是沒有進入case('formLoginUser')位?如果使用'$ _REQUEST'確保不使用獲取變量(或cookie)覆蓋post變量,那麼您可能會遇到問題。同樣,不是所有的服務器設置都允許你使用'$ _REQUEST'。有一個我們可以看到的工作示例嗎? –
該動作設置正確,它是「includes/ajax.php?action = formLoginUser」。在我的ajax.php中,我會在兩種情況下返回一個json對象,就像我在代碼中看到的那樣。在使用ajax提交表單之前,我使用了$ _REQUEST,沒有任何問題,因此應該沒有問題。除非ajax不能很好地與$ _REQUEST相處。 – bikey77
嗯所以這一切都沒有Ajax的作品?有沒有JavaScript錯誤?成功功能是否回撥? (添加alert('成功');'回調函數的開始) –