在使用jquery .ajax()重定向的POST之后获取实际的URL [英] Getting the actual URL after a POST which redirected using jquery .ajax()

查看:368
本文介绍了在使用jquery .ajax()重定向的POST之后获取实际的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Web应用程序中遵循PRG(重定向后获取)模式,并且使用类似以下的方法来执行大多数POST:

I am following the PRG (Post-Redirect-Get) pattern in my web application, and use something like the following for doing most of my POSTs:

$.ajax({
    type: 'POST',
    url: 'A.html',
    data: '....',
    statusCode: {
        302: function() {
            alert("302"); // this is never called
        },
        200: function() {
            alert("200");
        },
    },
    success: function (data, textstatus) {
        alert('You are now at URL: ' + ??);
    },
    error: function (data) {
    },
    complete: function (jqXHR, textstatus) {
        alert('You are now at URL: ' + ??);
    },
});

发生任何重定向后,我需要获取URL,即.ajax()函数调用的最终GET的URL.例如,到A.html的POST可以重定向到B.html或C.html(始终通过302).我如何获得最终的URL?

I need to get the URL AFTER any redirection has occurred, i.e. the URL of the final GET that the .ajax() function called. For example a POST to A.html may redirect to either B.html or C.html (always via 302's). How do I get the final URL?

我正在使用jquery 1.5.1,并且使用代理见证了jquery一直在默默地遵循重定向-我对此感到满意.我不在乎任何以302响应的URL-我只想知道在触发.ajax()的"success:"或"complete:"钩子时最终请求的URL.

I am using jquery 1.5.1, and using a proxy have witnessed that jquery is silently following the redirects - which I am happy with. I don't care about any of the URLs which responded with 302's - I would just like to know the URL of the final request at the time that .ajax()'s "success:" or "complete:" hooks are fired.

推荐答案

我终于通过在我的所有响应中添加一个附加标头(例如"X-MYAPP-PATH:/Admin/Index")解决了这个问题.

I finally solved this issue by adding an additional header into all my responses (eg "X-MYAPP-PATH: /Admin/Index").

因此,我的javascript可以更改为以下内容:

My javascript could thus be changed to the following:

success: function (data, textstatus, xhrreq) {
    alert('You are now at URL: ' + xhrreq.getResponseHeader("X-MYAPP-PATH"));
},

但是我仍然相信jquery应该能够为我提供当前的URL,所以我认为这是一个hack.

I still believe however that jquery should be able to give me the current URL, so I consider this a hack.

这篇关于在使用jquery .ajax()重定向的POST之后获取实际的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆