greenbandit
greenbandit

Reputation: 2275

ajax catch correct error code

i have this little code to post to my server

    $.ajax({
      type: 'POST',
      url: 'post.php',
      data: { token: '123456', title: 'some title', url: 'http://somedomain.com', data: '' },
      success: function(data){
        alert (data)
      }
    });        

Wondering how i can "catch" the different errors for ajax request: for eg, post.php return 'token error' when invalid token has been posted, or 'invalid title' for missing title.

Thanks in advance

Upvotes: 1

Views: 459

Answers (4)

Gabriel
Gabriel

Reputation: 1843

You probably want to set the 'error' handler function, which would treat the error you received.

Take a look at http://api.jquery.com/jQuery.ajax/ in the error setting.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

// build the initial response object with no error specified
$response = array(
  'error' => null
);

// the data checks went fine, process as normal
if (data is ok) {
  $response['some_object'] = value;

// something is bad with the token
} else if (bad token) {
  $response['error'] = 'token error';

// something is bad with the title
} else if (bad title) {
  $response['error'] = 'bad title';

// some other error occured
} else {
  $response['error'] = 'unspecified error';
}

// output, specifying that it's JSON data being returned
header('Content-Type: application/json');
echo json_encode($response);

and....

// $.ajax({ ...
success: function(data){
  if (!data.error){
    alert('OK!');
  }else{
    alert('Error: '+data.error);
  }
}
// });

Something like that perhaps? (Unless you're talking legitimate AJAX errors, in which case supply the error: function(x,t,e){} ajax option or use .ajaxError)

Upvotes: 1

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

What I like to do is set the dataType for the $.ajax() to 'json' and then in the PHP page you can just echo a json_encode()ed associative array. This will let you reference the values via your function parameter data (i.e. data.success, data.message). Let me know if you need some examples.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039428

If the server sends something else than 200 status code you could use the error handler:

$.ajax({
    type: 'POST',
    url: 'post.php',
    data: { 
        token: '123456', 
        title: 'some title', 
        url: 'http://somedomain.com', 
        data: '' 
    },
    success: function(data){
        alert(data);
    },
    error: function() {
        alert('some error occurred');
    }
}); 

If your server performs some validation on the request arguments maybe it could return a JSON object containing the error information (and set the proper Content-Type: application/json):

{ error: 'some error message' }

In this case you could handle this in the success callback:

success: function(data) {
    if (data.error != null && data.error != '') {
        // TODO: the server returned an error message
        alert(data.error);
    } else {
        // TODO: handle the success case as normally
    }
}

Upvotes: 2

Related Questions