How to get jQuery.ajax response status?
Asked Answered
S

2

9

Is there any way to know if the jqXHR object returned was redirected from another request? For example, having the following code:

   jQuery.ajax({
            type:       'POST',
            url:        'http://example.com/page.html',
            complete:   function(response, status) {
                        console.log(response.statusCode);
                        // do something
                        }
        });

If the ajax url is redirected with a 301 or 302 status code, the response will be the result of the redirected page and will return a 200 (OK) status code. I think this is a jQuery's bug (don't really know if this is the intended beheavior, since the ultimate response was actually succesful) I can't either get the original response Headers to check if there exists a Location Header, since this is only available on redirected requests.

Firebug with the above code logs these two requests through XHR

http://example.com/page.html 302 (Moved temporarily) http://example.com/redirect.html 200 (OK)

But jQuery only return data from the second request.

Maybe there is a way to get data from the first request? Or to get the response url and compare it with the one from the original request?

Skyeskyhigh answered 2/8, 2011 at 3:36 Comment(0)
H
3

The issue is that a 301 or 302 is handled directly by the browser due to standards compliance. This status code is not passed on to your ajax call. The only way that you know that the call has completed is in the complete handler, and furthermore there is no useful status given in the jqxhr object passed to complete. The Success and Error handlers will never get called.

We had some legacy ruby camping code that used a redirect to the get() method when a post() method was called so that we could display an updated view via ruby of the resource. When we switched to JavaScript views from camping views, the 301/302 was not passed to the jqxhr object. We had to rewrite the post methods to return 200 to fix the issue.

Here are some stackoverflow posts on the subject.

How to manage a redirect request after a jQuery Ajax call

Catching 302 FOUND in JavaScript

HTTP redirect: 301 (permanent) vs. 302 (temporary)

Hawkbill answered 2/8, 2011 at 22:35 Comment(0)
I
0

A tool i found that might possibly be able to help is wireshark. It's free and may be able to find out what is happening with the jqXHR object. It's fairly easy to use but follow these instructions:

http://www.wireshark.org/ Wireshark is a network protocol analyzer for Unix and Windows. I have only brushed the surface with this tool so far but it comes in handy when performing analysis on requests between your machine and others on the network.

Before you start - Wireshark captures every request so only click start when you are ready to capture and click stop when your finished. - You'll need your IP address

Read more about analysing HTTP requests

Inhalation answered 2/8, 2011 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.