我正在使用Laravel框架,並且正在發送AJAX請求來發送電子郵件。請求正常工作,併發送郵件,問題是如果郵件已成功發送,我無法獲得服務器響應。Laravel AJAX無法獲得回覆
下面的代碼(精簡版)至極坐落在景色/聯繫人/ mail.blade.php:
if(mail($to, $subject, $body,$headers)) {
$data = array('text' => Lang::line('contact.mail-success')->get());
return Response::json($data);
} else {
$data = array('text' => Lang::line('contact.mail-error')->get());
return Response::json($data);
}
,這裏是jQuery的:
$('#contact-form').submit(function() {
var request = $.ajax({
url: BASE+'/contact',
type: 'post',
data: { name: $('#name').val(), mail: $('#email').val(), message: $('#msg').val() },
dataType:"json",
success: function(data){
var message = $.parseJSON(data);
alert(message.text); // here I get the "cannot read property of null" in the console log
}
});
return false;
});
我在做什麼錯?謝謝你的幫助。
是什麼控制檯中的ajax調用的原始響應?你可以檢查嗎?我認爲在刀片視圖中放置「返回」聲明並不是正確的做法。 – migg
使用console.log(數據),我得到一個「空」的答案。 – SimCity