Parsing jQuery.ajax error message

I am sending data from one ASP.NET page to another through a jQuery ajax call in JSON form.

I simulate a situation where an error occurs when calling ajax. I get a response message in case of an error, and I need to assign this html to an element on the page.

Here is what I get in the message: jquery.ajax response

I have a javascript msg variable that when viewed through the Chrome debugger shows me that it contains the information I need in a responseText .

How do I get the responseText value to display on the page?

+8
javascript jquery
source share
4 answers

In variable names, JavaScript is case sensitive. In your example, you tried to access the responseText field of the msg object, but you had the capital "R". Try instead:

 msg['responseText'] 

Or in a much better style:

 msg.responseText 
+14
source share

Because its object uses dot notation to access it, e.g. xhr.responseText

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

You can see in the code only under the mouse pointer - only with "r" and not with "R":

 msg['responseText'] 
+2
source share
 <div id='error'></div> 

suppose u got an error in msg

 $('#error').html(msg.responseText) 
+1
source share

All Articles