jquery 判断 重定向url_在使用jquery .ajax()重定向的POST之后获取实际的URL

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: ' + ??);

},

});

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?

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.

解决方案

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

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"));

},

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值