当我使用Jquery执行ajax请求时,从PHP获取响应的正确方法是什么?Jquery Ajax响应
我有jQuery的验证码
$('#quote-form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: $(this).serialize(),
dateType: 'json',
url: 'mail.php',
success: function(data) {
alert(data.msg);
}
});
return false;
});
和PHP代码mail.php
:
// Some mail functions here
$mailSent = @mail($to, $subject, $message, $headers);
$return['msg'] = $mailSent ? 'mail sent' : 'mail failure';
echo json_encode($return);
我试图提醒效应初探alert(data.msg)
但它说不确定。有什么想法吗?
看看有哪些开发人员工具可供您查看正在返回的请求。如果你到达'alert',那么请求就会恢复正常。你只需要检查'数据'的样子(在Chrome中做一个console.log(data)来查看它) –
你可以尝试提醒(数据)吗? :) –
您需要使用['.parseJSON()'](http://api.jquery.com/jQuery.parseJSON/)成功解析您的JSON,或尝试使用['.getJSOn()'](http ://api.jquery.com/jQuery.getJSON/)而不是'.ajax()' – JMax