Jquery Ajax响应

Tur*_*tel 3 php jquery

当我使用Jquery执行ajax请求时,从PHP获取响应的正确方法是什么?

我有这个代码用于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;
});
Run Code Online (Sandbox Code Playgroud)

和PHP代码mail.php:

// Some mail functions here

$mailSent = @mail($to, $subject, $message, $headers); 
$return['msg'] = $mailSent ? 'mail sent' : 'mail failure';  
echo json_encode($return);
Run Code Online (Sandbox Code Playgroud)

我试图提醒响应alert(data.msg)但是它说未定义.有什么想法吗?

yak*_*aka 6

除了我没有测试的其他东西,但我刚看到你的ajax选项中有拼写错误:

dateType: 'json'
Run Code Online (Sandbox Code Playgroud)

将其更改为:

dataType: 'json'
Run Code Online (Sandbox Code Playgroud)