Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Jquery ajax function

JS code:

$.ajax({


        type: 'POST',
         url: 'http://localhost/MyServiceDir/Service.asmx/Foo',
         contentType: 'application/json; charset=utf-8',
         data: jsonData,
         success: function (msg) {
             alert("good");
         },
         error: function (xhr, status) {
              switch (status) {
                 case 404:
                     alert('File not found');
                     break;
                 case 500:
                     alert('Server error');
                     break;
                 case 0:
                     alert('Request aborted');
                     break;
                 default:
                     alert('Unknown error ' + status);
             } 
         }
     });

I get "unknown error error". How do I get to the bottom of this? I would like to know what the error actually is. Thanks!

like image 627
sarsnake Avatar asked Jan 18 '11 19:01

sarsnake


People also ask

How to debug AJAX call in jQuery?

In your page press f12 in mozilla and firebug will open. so by going in response and html you can see which response you are getting after your ajax call.

Is debugging difficult in AJAX?

It's very difficult to debug an AJAX request using the jQuery's shorthand AJAX methods because these methods don't give you a full control over the request.


3 Answers

The "status" parameter only includes why it failed -- timeout, error, etc... To get the status code you need to check the response object: xhr.status

See http://www.w3.org/TR/XMLHttpRequest/#response for details.

If you are getting "500 Internal Server Error" that is all you are going to get from ajax. You will have to check your application or server logs. This could be a syntax error or or library error or something else along those lines.

like image 56
Ben Lee Avatar answered Sep 20 '22 20:09

Ben Lee


Check xhr.status.

like image 22
SLaks Avatar answered Sep 23 '22 20:09

SLaks


Try this out in the onerror event:

alert(xhr.statusText)

EDIT:

I think your best bet here would be to install the FireBug Plugin on Firefox. This will allow you to see the ajax calls(enable "console" tab for this), responses, and error messages. Hopefully you will be able to get the info you need in this manner. This has been always been my method of choice for debugging ajax calls

like image 43
stephen776 Avatar answered Sep 22 '22 20:09

stephen776