来自服务器ajax的答复

I have some server side code that, upon the checkout being fulfilled returns either a true or a false. Through the console log it looks like this {"success": true, "message": "Added to db"}. How can I correctly write in the ajax success condition that if the answer is true, then we do something, otherwise we do something else?

handler php

        echo json_encode(['success' => true, 'message' => 'Added to db']);
    } 
} else { 
   echo json_encode(['success' => false, 'message' => 'Wrong captcha']);
}

index.php

$(document).ready(function() {
    $('#submit').click(function() {
        var username = $('#username').val();
        var email = $('#email').val();
        var message = $('#message').val();
        var captcha = $('#captcha').val();

        if(username === ''){
            alert('Please input data in all fields');
        } else {
            $.ajax({
                type: "POST",
                cache: false,        
                url: '/data/insert.php',
                data: {username: username, email: email, message: message, captcha: captcha},
                success: function(response) {
                    console.log(response);
                }
            });
        }
    });
});

Ajax will look at the http headers to determine if there was an error. You can set the error codes on your server when you're validating the response:

if ($someCondition) { // If the request is good
    echo json_encode(['message' => 'Added to db']);
} else { // Request is bad. You can add any number of conditions and return any number of errors
    header('HTTP/1.1 400 Bad Request');
    exit(json_encode(['message' => 'Some error message', 'code' => 400]));
}

You can see a list of client errors here

Then for your ajax there's a specific error handler function:

$.ajax({
        type: "POST",
        cache: false,        
        url: '/data/insert.php',
        data: {username: username, email: email, message: message, captcha: captcha},
        success: function(response) {
          console.log(response.message);
        },
        error: function(response) {
          alert(response.message);
        }
});
 $(document).ready(function() {
        $('#submit').click(function() {
            var username = $('#username').val();
            var email = $('#email').val();
            var message = $('#message').val();
            var captcha = $('#captcha').val();
            if(username === ''){
                alert('Please input data in all fields');
            }
            else {
            $.ajax({
                type: "POST",
                cache: false,        
                url: '/data/insert.php',
                data: {username: username, email: email, message: message, captcha: captcha},
                success: function(response) {
                 if(response.success == true){
                     // do some thing 
                 }else{
                     // do some thing  
                 }
                }
            });
        }
        });
    });