How to Get the Jquery $.Ajax Error Response Text

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

Try:

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

How to extract responseText from ajax error response?

The argument named err in your error handler is the jqXHR object. To retrieve the error message you return from your server side code you need to access the responseText property of that object manually. Try this:

error: (jqXHR) => {
console.log('an error occurred:', jqXHR.responseText);
}

jQuery.ajax - Get error message (Exception.Message) which is response from code behind

The response you received was just a string. So, you need to parse it to an object before assigning.

var responseText = '{"Message":"Attempted to divide by zero.","StackTrace":"...","ExceptionType":"System.DivideByZeroException"}';
console.log(JSON.parse(responseText).Message);
// in your case, it should be:// alert(JSON.parse(xhr.responseText).Message);

Find title/any tag of Ajax error.ResponseText from it's Html string or Converted jQuery Object

You can use $.parseHTML to convert html string to array of DOM nodes. Which can be used along with jquery selectors and function:

 var dom_nodes = $($.parseHTML(e.responseText));
alert( dom_nodes.filter('title').text());

Get server response with AJAX error?

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

How to correctly display jQuery Ajax Error response text as Alert

What do you receive as an error response is a json string. It is a stringified json object. You have to parse it to get an acces to json properties. After this you can join all errors messages from array that contains error messages ( in this case only one, but since it is an array it can contain more then one)

const err= JSON.parse(response.responseText).form_error.__all__.join("\n");
alert(err);

Properly displaying ajax error.responseText

In the end I returned JsonResults and handled the message from the view:

Server code:

[HttpPost]
[AllowAnonymous]
public virtual JsonResult AuthenticateUser(string somedata)
{
if (ok)
return new JsonResult() {Data="ok"}
return new JsonResult() {Data= "Missing data blah"}
}

And in javascript:

success: function (result) {
if (result == 'ok') {
window.location = '@Url.Content(Request["returnUrl"])';
return;
}

$('#progress_message').text("");
$('#errorMessage').text(result);
},


Related Topics



Leave a reply



Submit