3

I have a uri that is passed to me in a rest call that is is a redirect url and I want to be able to intercept the call to get the redirect 'location' in the header.

The following code returns null for the Location Header. What is the proper way to perform this client side?

$(document).ready(function () {
    $.ajax({
        url: 'http://myserver.com/redirect?token=AAAnY13Y-76LZ4zXDOd',  // obviously not cross domain and contains redirect location to a youtube link to embed in my page, http://www.youtube.com/embed/bl2j345-xy
        data: {
            something: 'someotherthing'
        },
        complete: function (request, status) {
            var location = request.getResponseHeader("Location");  // this is null...it shows up in Fiddler as http://www.myotherserver.com in the Location Header
        },
        error: function (request, error) {
            alert(request.responseText);
        }
    });
}); 
genxgeek
  • 13,109
  • 38
  • 135
  • 217
  • possible duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) Also related: http://stackoverflow.com/q/282429/995876 – Esailija Aug 10 '12 at 22:29
  • I'm not sure if it is just for me but the `status` = `error`. Could that be the reason you have no response headers? – Nope Aug 10 '12 at 22:34
  • @FrançoisWahl: It's true, I just checked and the status is error. It was working before but definitely erring but request.responseText is blank. – genxgeek Aug 10 '12 at 22:51
  • @JaJ: When you find out what the cause was and the solution, please post it, I think it would be interesting to see what causes the empty request header. – Nope Aug 12 '12 at 10:37

2 Answers2

0

why don't you set -> http://myserver.com/redirect?token=AAAnY13Y-76LZ4zXDOd to return the redirect location http://www.myotherserver.com and so :

complete: function (request, status) {
window.location.href=request;
}
Kris Khairallah
  • 1,537
  • 13
  • 16
0

Using this fiddle. I updated your code ever so slightly, adding some console outputs for debugging as I currently always get error: NETWORK_ERR: XMLHttpRequest Exception 101.

The main change I made, which I think may be the cause for your empty headers, was setting async: false. This is important as you do not want the request to return before it has been completed. The default setting is async: true if not specified, which causes the ajax call to immediately to return which is probably why you will see no response headers even if the call is successfull.

I also added a success: function... callback with a simple debug to ensure it get caught if it goes in there.

Also check out the part in the ajax documentation on crossDomain non-async requests and the crossDomain settings:

async
Default: true
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

crossDomain(added 1.5)
Default: false for same-domain requests, true for cross-domain requests
If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain.

I'm not sure if a redirect is considered cross-domain but I'm guessing a combination of the above is the cause for the error and empty request headers.

Nope
  • 22,147
  • 7
  • 47
  • 72
  • Cool, thanks! However, I do get an error as logged per console...there's not a lot of error information in there: Error-StatusText: error Complete-Status: error Complete-StatusText: error Complete-ResponseHeaders: – genxgeek Aug 10 '12 at 23:27
  • @JaJ: If you place a `debugger;` line at the top of the complete and/or error callbacks the script will stop there and you can use the console then to look at the full request objects, just type `request` into the console to get the object. In Chrome for example you can nicely traverse through the complete object properties, and see the methods which you can call in the console then too. – Nope Aug 10 '12 at 23:51
  • @Jaj: Out of curiosity, can you access the main url directly in a browser from your PC? Additionally, are you running this as a intranet site with windows authentication? Maybe there is an access issue when the code is trying to access the url? I'm only thinking out loud :) – Nope Aug 10 '12 at 23:56
  • Thanks for the replies. I'm still looking into this and trying to come to a collusion. Will keep this thread updated. Appreciate the help! – genxgeek Aug 15 '12 at 14:51