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:
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?
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 .
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.
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.
A 404 also means, your JSP code never gets executed.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With