Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a jQuery.ajax error response message

I am posting data from one ASP.NET page to another via jQuery ajax call in a form of JSON.

I am simulating the situation, where is get an error on ajax call. 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 a message: jquery.ajax response

I have the msg javascript variable, that, when looked up via Chrome debugger shows me that it contains info I need in responseText.

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

like image 240
Maxim V. Pavlov Avatar asked Oct 27 '11 14:10

Maxim V. Pavlov


People also ask

What is parse error in AJAX call?

The reason why this parsererror message occurs is that when you simply return a string or another value, it is not really Json , so the parser fails when parsing it. So if you remove the dataType: json property, it will not try to parse it as Json .

How can I get response data from AJAX call?

You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.

What triggers AJAX error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

What is an AJAX error 404?

A 404 also means, your JSP code never gets executed.


2 Answers

In JavaScript variable names are case sensitive. In your example, you were trying to access the responseText field on the msg object, but you had a capital 'R'. Try this instead:

msg['responseText']

Or in much better style:

msg.responseText
like image 131
a'r Avatar answered Nov 15 '22 22:11

a'r


Since its an Object use the dot notation to access it like xhr.responseText

error: function(xhr, status, error) {

  var err = eval("(" + xhr.responseText + ")");

  alert(err.Message);

}
like image 43
Michael D. Irizarry Avatar answered Nov 15 '22 21:11

Michael D. Irizarry