jQuery AJAX not redirecting?

Posted by MidnightLightning on Stack Overflow See other posts from Stack Overflow or by MidnightLightning
Published on 2011-01-13T21:18:50Z Indexed on 2011/01/13 21:54 UTC
Read the original article Hit count: 376

Filed under:
|
|

I have a simple PHP page (for testing) that simply calls header("Location: http://www.example.com");exit;, which resides on the same server, in the same directory as another file with the following jQuery Javascript:

$(document).ready(function() {
    jQuery.ajax({
        type : 'GET',
        url : 'bounce.php',
        error : function(xhr, status, error) {
            console.log("ERROR: ", xhr, xhr.status, xhr.getAllResponseHeaders());
        },
        complete : function(xhr, status) {
            // Get headers of the response
            console.log("COMPLETE: ", xhr, xhr.status, xhr.getAllResponseHeaders());
        }
    });
});

I was expecting (from several other StackOverflow responses) for the xhr.status to return "302", but instead the AJAX call is triggering the "error" event (and then the "complete" event), and xhr.status is returning 0 (zero), and .getAllResponseHeaders() is coming back null (in both the error, and complete functions).

Firebug is showing the "302 Moved Temporarily", and the response headers. So why is this triggering the error event, and not passing along the proper 302 code, and headers? Is this something to do with the Same Origin since the bouncing script and the fetching script are both on the same server? Is this jQuery or Javascript's fault?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery