如何让PHP AJAX错误出现在我的jQuery代码中?

Gee*_*Out 7 javascript php ajax jquery

我有一些PHP AJAX代码,它应该验证jQuery发送的一些参数并返回一些值.目前,它一直返回调用jQuery错误的情况,我不知道为什么.

这是我的jQuery代码:

$('.vote_up').click(function() 
{        
    alert ( "test: " + $(this).attr("data-problem_id") );
    problem_id = $(this).attr("data-problem_id");

    var dataString = 'problem_id='+ problem_id + '&vote=+';

    $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(json)
                {           
                    // ? :)
                    alert (json);


                },
                error : function(json) 
                {
                alert("ajax error, json: " + json);

                //for (var i = 0, l = json.length; i < l; ++i) 
                    //{
                    //  alert (json[i]);
                    //}
                }
            });


    //Return false to prevent page navigation
    return false;
});
Run Code Online (Sandbox Code Playgroud)

这是PHP代码.PHP中的验证错误确实发生了,但是我没有看到php侧发生的错误是调用jQuery错误情况的错误.

这是被调用的片段:

if ( empty ( $member_id ) || !isset ( $member_id ) )
{
    error_log ( ".......error validating the problem - no member id");
    $error = "not_logged_in";
    echo json_encode ($error);
}
Run Code Online (Sandbox Code Playgroud)

但是如何在我的jQuery JavaScript中显示"not_logged_in"以便我知道它返回的位是什么?如果不是,我该如何确定该错误是回到jQuery的?

谢谢!

com*_*omu 7

不要在json_encode()方法中回显$ error,只需回显$ error就像这样.另外,不要使用变量json,使用变量数据.编辑代码如下:

PHP

if ( empty ( $member_id ) || !isset ( $member_id ) )
{
    error_log ( ".......error validating the problem - no member id");
    $error = "not_logged_in";
    echo $error;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.vote_up').click(function() 
{        
    alert ( "test: " + $(this).attr("data-problem_id") );
    problem_id = $(this).attr("data-problem_id");

    var dataString = 'problem_id='+ problem_id + '&vote=+';

    $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    // ? :)
                    alert (data);


                },
                error : function(data) 
                {
                alert("ajax error, json: " + data);

                //for (var i = 0, l = json.length; i < l; ++i) 
                    //{
                    //  alert (json[i]);
                    //}
                }
            });


    //Return false to prevent page navigation
    return false;
});
Run Code Online (Sandbox Code Playgroud)