Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get server response with AJAX error?

Tags:

json

jquery

ajax

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);   }, 
like image 979
Ziplo Avatar asked Oct 16 '13 13:10

Ziplo


People also ask

How does error handle response in AJAX?

When there is an AJAX error response or the AJAX request times out, you'll want to log as much information as you have, including the error message that jQuery gives you, the url and the request data. $. ajax(url, { "data": requestData, "type": "POST", "timeout": 5000 }) .

What is 500 Internal server error AJAX?

The 500 Internal Server Error is a very general HTTP status code. It means something has gone wrong on the website and webserver is unable to specify what exactly, thus failing in fulfilling the request made by the client.


1 Answers

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?)

like image 96
sk8terboi87 ツ Avatar answered Sep 22 '22 16:09

sk8terboi87 ツ