0

I'm trying to pass custom error code to the client-side to ajax error function. In the server side:

$response = array();

if ( empty($post['parent_id']) ) {
    $response = array('error' => true, 'status_code' => -2);
    exit();
}

$is_valid_id = RC()->is_valid_id($post['parent_id']);
$row = RC()->get_row_data($post['parent_id']);
if ( ! $is_valid_id ) {
    $response = array('error' => true, 'status_code' => -1);
} else if ( ! $row ) {
    $response = array('error' => true, 'status_code' => 0);
} else {
    $response = json_encode($row);
}

echo $response;

Then I want to check for this status code in my js script, but couldn't find a way to do this (found ways only without trigger the error event).

$.ajax({
    url: ajax_url,
    data: {
        'action': 'rc_parent_sign_in',
        'form_data': $('#parent-sign-in-form').serialize(),
        'security': security_nonce
    },
    type: "post",
    dataType: "json",
    cache: false,
    success: function (response) {
        var query_vars = $.param(response);
        window.location.replace('http://localhost/renecassin/user-registration/?' + query_vars);
    },
    error: function (response) {
        $('.form-control-feedback').addClass('hide');

        /* Looking for something like this */
        switch ( response.status_code) {
            case -2 :
                parent_id_form_group.addClass('has-danger').children('#empty-field').
                removeClass('hide');
                prent_id_input.addClass('form-control-danger');
                break;
            case -1 :
                parent_id_form_group.addClass('has-danger').children('#not-valid-id-feedback').
                removeClass('hide');
                prent_id_input.addClass('form-control-danger');
                break;
            default :
                parent_id_form_group.addClass('has-danger').children('#id-not-exists-feedback').
                removeClass('hide');
                prent_id_input.addClass('form-control-danger');
        }
    }
});

Any help will be appreciate.

Avishay28
  • 2,288
  • 4
  • 25
  • 47

1 Answers1

2

It's because the response will go to your response callback, you are successfully returning an object.

The error callback will only be called if the request itself failed (timeout,404 etc..)

You need to handle your internal error codes in your success callback

naortor
  • 2,019
  • 12
  • 26
  • The response does trigger the error function, however the property `status_code` is empty. – Avishay28 Apr 30 '17 at 08:23
  • so it's because your are not returning it right - on code -2 you are returning nothing - what causes a timeout, and on the other 2 errors it might be because you don't parse it - simply adding an error key true to your object wont make your response go to error callback – naortor Apr 30 '17 at 08:27
  • Ok, I handle the error in the success callback. Just though it make more sense to use the error callback for the errors.. – Avishay28 Apr 30 '17 at 12:39
  • if you really insist you can change the response status http://stackoverflow.com/a/12018482/4819077 in order to get to the error callback function – naortor Apr 30 '17 at 13:30