Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery How to find out what the ajax error is?

Tags:

I have the following bit of code which I'm just trying out by running in firebug

$.ajax({   type:"POST",   url:"http://mpdomain/WebService.asmx/Operation",   data: "{'parameter1': '44906'}",    contentType: "application/json;charset=utf-8",   dataType: "json",   success: function(data) { alert("succsess") },   error: function(e, ts, et) { alert(ts) } }) 

In theory it should work. However the error handler is triggered, and ts is simply set to "error". How do I get more detail about what's gone wrong?

like image 740
George Mauer Avatar asked Feb 05 '10 19:02

George Mauer


People also ask

How do I know if Ajax call failed?

ajax() : $. ajax({ type: 'POST', url: 'page. php', data: stuff, success: function( data ) { }, error: function(xhr, status, error) { // check status && error }, dataType: 'text' });

How do I know if jQuery Ajax is working?

You can show your loader / waiting image in ajax request in this way. $('#loading-image'). show(); $. ajax({ url: uri, cache: false, success: function(html){ $('.

What triggers jQuery 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.


2 Answers

To see the error message from an AJAX request, you can do something like this:

$.ajax({   type:"POST",   url:"http://mpdomain/WebService.asmx/Operation",   data: "{'parameter1': '44906'}",    contentType: "application/json;charset=utf-8",   dataType: "json",   success: function(data) { alert("success") },   error: function(ts) { alert(ts.responseText) } // or console.log(ts.responseText) }); 

Note that, inside the error function, you get the responseText.

like image 184
ozsenegal Avatar answered Nov 12 '22 16:11

ozsenegal


The error message jQuery gives you is not very descriptive. It can either be "timeout", "error", "notmodified" or "parsererror." http://api.jquery.com/jQuery.ajax/ so what you can conclude is that it's not a timeout, not modified or parse error that you are getting.

Make sure in Firebug you see the request set to the correct address and the correct data is being set. You can also view the response so if you also have access to the server code a quick and dirty way is just to echo what is going on server side and view the response with Firebug.

Also I'm not sure if this is an issue but try to set the data to {parameter1: 44906} (basically remove the quotes so you are passing in an object and not a string).

like image 42
jhchen Avatar answered Nov 12 '22 18:11

jhchen