最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Capture redirect location of javascript XMLHttpRequest - Stack Overflow

programmeradmin2浏览0评论

I know that you can't, when using an XMLHttpRequest, intercept a redirect or prevent it, as the browser will transparently follow it, but is it possible to either

A. Determine whether a request redirected, or

B. Determine where it redirected to? (assuming that the response gives no hints)

Example code:

$.post("/my-url-that-redirects/", {}, 
    function(response, statusCode, xmlHttpRequest){
        //Somehow grab the location it redirected to
    }
);

In my case, firebug will first show a POST to the url, then a GET to the redirected url. Can that GET location be captured?

I know that you can't, when using an XMLHttpRequest, intercept a redirect or prevent it, as the browser will transparently follow it, but is it possible to either

A. Determine whether a request redirected, or

B. Determine where it redirected to? (assuming that the response gives no hints)

Example code:

$.post("/my-url-that-redirects/", {}, 
    function(response, statusCode, xmlHttpRequest){
        //Somehow grab the location it redirected to
    }
);

In my case, firebug will first show a POST to the url, then a GET to the redirected url. Can that GET location be captured?

Share Improve this question asked Dec 16, 2010 at 21:29 SneaSnea 1,9752 gold badges14 silver badges21 bronze badges 3
  • You just get what is in the response headers. – epascarello Commented Dec 16, 2010 at 21:40
  • 1 xmlHttpRequest.getAllResponseHeaders() doesn't seem to give any indication of the URL it was for. – Snea Commented Dec 16, 2010 at 21:41
  • 3 Have somebody found any workaround on the client-side? How to capture a redirect url if don't have a control of a web server. – Pavel Alexeev Commented Mar 28, 2012 at 13:07
Add a comment  | 

2 Answers 2

Reset to default 4

1) Use different status code than 301 (2**) (if request by ajax) and handle redirection on client side:

var STATUS = {
  REDIRECT: 280
};

$.post('/redirected', {}, function(response, status, request) {
  if (status == STATUS.REDIRECT) {
    // you need to return the redirect url
    location.href = response.redirectUrl;
  } else {
    $('#content').html(request.responseText);
  }
});

2) DO NOT REDIRECT:

I use that in "redirect pattern" = redirecting after post request (you don't want to allow user to refresh the post request, etc..)

With ajax request, this is not necessary, so when the post request is ajax, I do forward instead (just forward to different controller - depends on your server-side framework, or what you are using...). POST requests are not cached by browsers.

Actually, I don't know what's the reason you need that, so this might not be so useful for you. This is helpful when server returns different responses for ajax requests than common requests, because when browser redirect ajax request, the redirected request is not XMLHttpRequest...

[updated]

You can access headers (of redirected request) like that:

$.post('redirected', {}, function(r, s, req) {
  req.getAllResponseHeaders();
  req.getResponseHeader('Location');
});

There should be 'Location' header, but it depends on the server, which headers are sent back...

After 4 years now it's possible to find the last redirect location using responseURL from XHR instance in Chrome 42+ (Opera 29+) and Firefox 39+ but it's not available in IE, Edge or safari yet.

发布评论

评论列表(0)

  1. 暂无评论