最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the PHP AJAX error to get to show up in my jQuery code? - Stack Overflow

programmeradmin4浏览0评论

I have some PHP AJAX code that is supposed to validate some parameters sent by jQuery and return some values. Currently, it consistently returns invokes the jQuery error case, and I am not sure why.

Here is my jQuery code:

$('.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;
});

and here is the PHP code. The validation errors in PHP do occur, but I see no sign that the error that is happening on the php side, is the one that is invoking the jQuery error case.

This is the snippet that gets invoked:

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

But how do I get the "not_logged_in" to show up in my JavaScript of the jQuery so that I know it is the bit that got returned? And if it isn't, how do I make it that that error is what comes back to the jQuery?

Thanks!

I have some PHP AJAX code that is supposed to validate some parameters sent by jQuery and return some values. Currently, it consistently returns invokes the jQuery error case, and I am not sure why.

Here is my jQuery code:

$('.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;
});

and here is the PHP code. The validation errors in PHP do occur, but I see no sign that the error that is happening on the php side, is the one that is invoking the jQuery error case.

This is the snippet that gets invoked:

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

But how do I get the "not_logged_in" to show up in my JavaScript of the jQuery so that I know it is the bit that got returned? And if it isn't, how do I make it that that error is what comes back to the jQuery?

Thanks!

Share Improve this question asked Oct 5, 2011 at 21:19 GeekedOutGeekedOut 17.2k38 gold badges112 silver badges189 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

Don't echo $error in the json_encode() method just simply echo $error like so. Also, don't use the variable json, use the variable data. Edited code below:

PHP

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

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;
});

jQuery uses the .success(...) method when the response status is 200 (OK) any other status like 404 or 500 is considered an error so jQuery would use .error(...).

You must handle all output returned from the php script in the success handler in javascript. So a not-logged in user in php can still (should normally...) result in a successful ajax call.

If you are consistently getting the error handler in your javascript call, your php script was not run or is returning a real error instead of a json object.

According to the manual, you have 3 variables available in the error handler, so just checking these will tell you exactly what the problem is:

// success
success: function(data)
{
  if (data == 'not_logged_in') {
    // not logged in
  } else {
    // data contains some json object
  }
},
// ajax error
error: function(jqXHR, textStatus, errorThrown)
{
  console.log(jqXHR);
  console.log(textStatus);
  console.log(errorThrown);
}
//

Easiest way to debug PHP in this case, is to modify .htaccess to create an error log.

php_flag log_errors on

发布评论

评论列表(0)

  1. 暂无评论