在JQuery中查找PHP返回AJAX的错误


Find error PHP returns to AJAX in JQuery

这是有效的:

 $.ajax({
            type: 'POST',
            url: 'register.php',
            data: {captcha: captcha
            },
            success: function() {
            $('#loading').hide();
            $('#success').fadeIn();
            } 
            error: function() {
            $('#loading').hide();
            $('#captcha').fadeIn();
            $('#catErrorB').fadeIn();
            } 
});     

将captcha响应发送到PHP,如果输入正确,则可以注册。问题是,如果您不正确地输入captcha,JQuery仍然会运行成功运行的函数,尽管PHP运行了"die",但什么也没做。

在PHP中,如果captcha输入错误,这个是不是

if (!$resp->is_valid) {
           die ("false");
  } 
  else 
  {
  register
  }

我如何请求PHP吐出的错误,以便我可以做类似的事情?

    success: function(find error) {
        if(error == "false")
        {
        $('#loading').hide();
    $('#captcha').fadeIn();
    $('#catErrorB').fadeIn();
        }
        else
        {
        $('#loading').hide();
        $('#success').fadeIn();
        }
        } 

编辑!:在你的帮助下,这就是它现在的样子,它非常棒!!

$.ajax({
            type: 'POST',
            url: 'register.php',
            dataType: "json",
            data: {
                challenge: challenge,
                response: response,
                zip: zip
            },
            success: function(result) {  
            if (!result.success) { 
            $('#loading').hide();
            $('#captcha').fadeIn();
            $('#catErrorB').fadeIn();
            } 
            else { 
            $('#loading').hide();
            $('#success').fadeIn();
    } 
} 


});         

和PHP

if (!$resp->is_valid) {
    $response = array(success => false); 
    echo json_encode($response); 
  } 
  else 
  {
$response = array(success => true); 
echo json_encode($response); 

你会是最酷的。我做过的第一个JQuery,它非常棒。此网站规则!

我不会使用成功/失败。即使脚本失效,它仍然会向您的ajax调用返回200个SUCCESS。

使用JSON根据是否成功返回响应,并解析并使用正确的逻辑

<?php
if ( $resp->is_valid ) {
  echo json_encode( array( 'status' => 'true' ) );
} else {
  echo json_encode( array( 'status' => 'false' ) );
}
?>

然后您可以在AJAX调用中解析响应。记住,如果您调用die('false');,它仍然会向您的ajax函数返回一条成功消息。

PHP:

if (!$resp->is_valid) {
    $response = array(success => false);
    echo json_encode($response);
}
else { 
    $response = array(success => true);
    echo json_encode($response);
}

jQuery:

success: function(result) { 
    if (!result.success) {
        // Wrong input submitted ..
    }
    else {
        // Correct input submitted ..
    }
}