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 に答える