Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle statuscode 307 redirect with jquery

I'm having a lot of trouble with the 307 Temporary Redirect statusCode using jquery.

I have an apache server that returns a 307 when needed. The 307 is getting thrown properly (I can see this with firebug or a wireshark trace), but I can't seem to get the redirect to trigger.

$.ajax({
    url: someURL,
    type: 'GET',
    processData: false,
    data: someData,
    cache: true,
    timeout: 5000,
    statusCode: {
        307: function() {
            alert('307'); // this does NOT get called
        }
    },
    error: function(request, status, error){
        try {
            console.log(request.getAllResponseHeaders());
        }
        catch (err) { }
    },
    success: function(data, textStatus, response){
        try {
            console.log(response.getAllResponseHeaders());
        }
        catch (err) { }
    });

I wanted to check out the headers so I could redirect using the getAllResponseHeaders() method. This works fine for 200's but the 307 never triggers (in the success nor error handler)

One thing I noticed when using Firebug to examine the traffic is that right after the redirect an ajax GET is sent to the server with the url of the redirect! This results in a failure however.

So, what gives?

Edit: I tried this with jquery 1.5.1 and 1.6.1

like image 259
Darcy Avatar asked Jun 13 '11 15:06

Darcy


1 Answers

Well, I did some more research and it appears that a 307 is automatically handled by the browser, and is not able to be intercepted by jquery.

I ended up using a different statusCode to do the redirect (410 Gone).

This probably isn't the best solution, but I don't have access to the apache server code.

like image 187
Darcy Avatar answered Sep 29 '22 20:09

Darcy