11

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.

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Check xhr.status.

2 Comments

it does give 500(server error)...but is there a way to get more specific details? or is it pretty much as info as I will get from $ajax?
@gnomixa: You can get whatever the server sends. Look at xhr.responseText.
3

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

3 Comments

thanks, but that's too generic also - "internal server error". I am looking for something more informative such as "wrong number of arguments passed"...not sure if it's possible.
Edited my answer above...FireBug may help you out
FireBug is buggy to install. It actually won't install on my FF 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.