如何正确地获取 PHP 对 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)
,但它显示为未定义。有什么想法吗?
.parseJSON()
解析您的 JSON 或尝试使用.getJSON()
代替.ajax()
。 - JMax(一个空字符串)
。 - TurteldataType: 'json'
,而他打错了,写成了dateType
。 - N.B.