javascript 如何让 PHP AJAX 错误显示在我的 jQuery 代码中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7667876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:52:22  来源:igfitidea点击:

How to get the PHP AJAX error to get to show up in my jQuery code?

phpjavascriptjqueryajax

提问by GeekedOut

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.

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

Here is my jQuery code:

这是我的 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;
});

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.

这是 PHP 代码。PHP 中的验证错误确实发生了,但我看不到 php 端发生的错误是调用 jQuery 错误案例的错误的迹象。

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?

但是如何让“not_logged_in”显示在我的 jQuery 的 JavaScript 中,以便我知道它是返回的位?如果不是,我如何使该错误返回到 jQuery?

Thanks!

谢谢!

采纳答案by comu

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:

不要在 json_encode() 方法中 echo $error 只是像这样简单地 echo $error 。另外,不要使用变量json,使用变量数据。编辑代码如下:

PHP

PHP

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

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

回答by veritas

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

.success(...)当响应状态为200(OK) 任何其他状态(如404或被500视为错误)时,jQuery 使用该方法,因此 jQuery 将使用.error(...).

回答by jeroen

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

您必须success在 javascript的处理程序中处理从 php 脚本返回的所有输出。因此,在 php 中未登录的用户仍然可以(通常应该......)导致成功的 ajax 调用。

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

如果您始终error在 javascript 调用中获取处理程序,则您的 php 脚本未运行或返回真正的错误而不是 json 对象。

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:

根据手册,错误处理程序中有 3 个变量可用,因此只需检查这些变量就可以准确地告诉您问题是什么:

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