Ziplo
Ziplo

Reputation: 385

Get server response with AJAX error?

for an incorrect Ajax action, I set with HTTP header code to 403 and send the following response :

{"code":"403","status":"Forbidden","message":"You cannot do this"}

However, I can't access this data when handling my error... Is it possible to acess "message"data from jqXHR ?

something like jqXHR.message ?

Many thanks for your help...

EDIt :

error: function (xhr) {
            $(".alert").html(xhr.responseText);
          },

This returns :

{"code":"403","status":"Forbidden","message":"You cannot do this"}

But xhr.responseText.message doesn't return anything ...

EDIT : this code works :

  error: function (xhr) {
    var jsonResponse = JSON.parse(xhr.responseText);
    $(".alert").html(jsonResponse.message);
  },

Upvotes: 27

Views: 82760

Answers (1)

sk8terboi87  ツ
sk8terboi87 ツ

Reputation: 3457

You should be getting in jQuery 'error' callback... http://api.jquery.com/jQuery.ajax/

 error: function(xhr, status, error) {
    alert(xhr.responseText);
 }

(btw.. ur code?)

Upvotes: 53

Related Questions