Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the jQuery $.ajax error response text?

People also ask

How do I return a response text in AJAX?

What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.

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 }) .


Try:

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

For me, this simply works:

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

Look at the responseText property of the request parameter.


As ultimately suggested by this other answer and it's comments on this page:

error: function(xhr, status, error) {
  var err = JSON.parse(xhr.responseText);
  alert(err.Message);
}