简体   繁体   English

如何使用 jquery 处理状态码 307 重定向

[英]How to handle statuscode 307 redirect with jquery

I'm having a lot of trouble with the 307 Temporary Redirect statusCode using jquery.我在使用 jquery 的307 Temporary Redirect状态码时遇到了很多麻烦。

I have an apache server that returns a 307 when needed.我有一个 apache 服务器,它在需要时返回 307。 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. 307 被正确抛出(我可以通过 firebug 或 wireshark 跟踪看到这一点),但我似乎无法触发重定向。

$.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.我想检查标题,以便可以使用getAllResponseHeaders()方法进行重定向。 This works fine for 200's but the 307 never triggers (in the success nor error handler)这适用于 200,但 307 从不触发(在successerror处理程序中)

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.在使用 Firebug 检查流量时,我注意到的一件事是,在重定向之后,ajax GET与重定向的 url 一起发送到服务器。 This results in a failure however.然而,这会导致失败。

So, what gives?那么,什么给了?

Edit: I tried this with jquery 1.5.1 and 1.6.1编辑:我用 jquery 1.5.1 和 1.6.1 试过这个

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.好吧,我做了更多的研究,似乎 307 是由浏览器自动处理的,并且无法被 jquery 拦截。

I ended up using a different statusCode to do the redirect ( 410 Gone ).我最终使用不同的 statusCode 进行重定向( 410 Gone )。

This probably isn't the best solution, but I don't have access to the apache server code.这可能不是最好的解决方案,但我无权访问 apache 服务器代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM