0

我在我的 ajax 请求中收到错误 200。我想在我的 ajax 请求中向页面发布 1 个值,这是一个电子邮件地址。有人可以告诉我出了什么问题

错误:

message=:[object Object], text status=:parsererror, error thrown:=SyntaxError: JSON.parse: unexpected end of data

jQuery

$('#checkemail').click(function() {
    $.ajax({
        url:'http://' +  location.host + '/buyme/include/getemailaddress.php',
        type:'POST',
        contentType: "application/json; charset=utf-8",

        data: {email:$('#email').val()},
        dataType:"json",
        success: function(msg) {
            alert(msg);
        },
        error: function(ms, textStatus, errorThrown) {
            alert(errorThrown); 
        }   
    });/**/
});
4

1 回答 1

1

当您使用 json 数据类型时,从服务器返回的任何数据都必须采用该格式。

所以首先不要忘记您发送post数据,因此请使用:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $email = $_POST['email'];
    // ...

    // and SECOND return suitable data type, ie, a json coded string.
    echo json_encode($your_result);

    // where $your_result can be simple as

    // $your_result['result'] = true;
    // $your_result['result'] = false;

    // a message
    // $your_result['msg'] = 'all OK';

    // a message and and a flag
    // $your_result['msg'] = 'all OK';
    // $your_result['result'] = true;
}

因此,在您的 jquery 回调中,您会得到如下返回的数据:

success: function(data) {
    if (data.msg != "") alert(data.msg);
    if (data.result === true) {} else {}
},
于 2013-02-04T03:38:19.997 回答