jQuery Ajax – how to get response data in error

Here’s an example of how you get JSON data on error:

$.ajax({
    url: '/path/to/script.php',
    data: {'my':'data'},
    type: 'POST'
}).fail(function($xhr) {
    var data = $xhr.responseJSON;
    console.log(data);
});

From the docs:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

Otherwise, if responseJSON is not available, you can try $.parseJSON($xhr.responseText).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)