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
How to Get Innerhtml of Domnode
Illegal String Offset Warning PHP
How to Prevent Form Resubmission When Page Is Refreshed (F5/Ctrl+R)
How to Sort an Array of Associative Arrays by Value of a Given Key in PHP
Pdo Prepared Inserts Multiple Rows in Single Query
Get Order Items and Wc_Order_Item_Product in Woocommerce 3
How to Generate Xml File Dynamically Using PHP
Ajax Request Callback Using Jquery
How to Change the Session Timeout in PHP
What Are the Best PHP Input Sanitizing Functions
Parse Xml With Namespace Using Simplexml
How to View Query Error in Pdo PHP
The Plugin Generated X Characters of Unexpected Output During Activation (Wordpress)
Access a JavaScript Variable from PHP
When Should I Use MySQLi Instead of MySQL
How to Use Password Hashing With Pdo to Make My Code More Secure
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate