11

I've a server that returns HTTP status code 200, 201, and 202 from the same url. In Chrome, I've confirmed with the Network debugging panel that the Status Code is what I expect it to be (i.e. 200, 201 or 202). I rely on that status code to determine the next step.

I'd expect that the callbacks for jQuery (version 1.5.2) AJAX requests to set jqxhr.status to the status code that the server sends. However, the status code is always 200, even if the code sent by the server is 201 or 202.

In other words, the following code prints Code: 200 regardless of what the server sends.

$.get(url, {}, function (data, textStatus, xhr ) {
    alert("Code: " + xhr.status);
});

Why is this happening, and more importantly, how can one get the actual status code in a jQuery AJAX callback for $.get or $.ajax?

Thank you for reading.

5
  • any chance you are using JSONP or cross-domain GET request? Commented Oct 28, 2011 at 16:16
  • Maybe this will be of some help: bugs.jquery.com/ticket/8211 Commented Oct 28, 2011 at 16:16
  • KrisIvanov: No JSONP or cross-domain. It's a request of the usual sort. Commented Oct 28, 2011 at 16:23
  • See this bug report and see if it's related; wondering what happens if you use a $.ajax and report the status in both the code handler and success. Commented Oct 28, 2011 at 16:25
  • Bug 8211 seems only tangentially related - i.e. jQuery doesn't properly handle statusCode and success handlers. This seems to be a strange truncation of the status code - I'd think there'd be some indication in e.g. github.com/jquery/jquery/blob/master/src/ajax.js, but I don't see anything that would obviously change the jqXHR.status to 200 from 201/202. Commented Oct 28, 2011 at 16:34

1 Answer 1

2

From what I have experienced jQuery is just not set up very well for handling actual status codes in the response. You can try just doing a manual AJAX call using some good old bare bones JS and handle the status yourself.

Here are a few tutorials on how to do so.

http://www.degraeve.com/reference/simple-ajax-example.php

http://www.w3schools.com/ajax/default.asp

request.status is where you should be able to access the status code in your request object. Here is another page showing a little bit about how to access even more granular information about the status of the request.

http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/

Hope that helps you nail it!

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

1 Comment

Thumallen is right, it's either all or nothing. Catching redirect via jquery ajax is unpleasant

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.