4

I am performing an Ajax request to a server that accepts cross-domain requests but for which I have no control over the server code. My desire is to extract an HTTP Link header from the response. As an example:

$.ajax({
 url: theURL
}).done(function(data,textStatus,xhr){});

hits a server that responds with the following (as observable when the URL is queried with curl):

HTTP/1.1 302 Found
Link: <http://thedataIwant.com>;rel="foo"
Location: http://someothersite.com

The browser follows the HTTP 3XX code and I get the contents of the HTTP headers from http://someothersite.com in the done() handler; however, I would like to first extract the contents of the Link header for the initial HTTP response with the 3XX code.

How do I go about extracting the contents of the HTTP Link header from an HTTP response with 3XX status code?

Mat Kelly
  • 2,327
  • 7
  • 29
  • 51

1 Answers1

3

I was intrigued by your question and though to search around for a solution. Unfortunately, there isn't a direct one. According to all posts I read so far (How to manage a redirect request after a jQuery Ajax call and many similar ones) you can't simply catch the 301 redirect because browsers usually fetch the content and give the endpoint to the user, which is why you get the 200 status code instead of the 302. As a workaround, people are suggesting to use a custom header. When you receive the header after doing an ajax request, you could do your own manipulation i.e. save the Link header and then make a second ajax request to get the content from MyRedirectLocationHeader: http://someothersite.com.

The code would look something similar to this:

$.ajax({
    url: theURL,
    success: function(response, status, xhr) {
        var link = xhr.getResponseHeader('Link');
        if(link != null) {
            // my second ajax request to the link in the MyRedirectLocationHeader
        }
    }
});

This is an awful looking hack, but that's the only workaround I've found so far which actually works. Another way might be for you to create a proxy script/service using PHP,Java or another similar language, that would get the request without following the redirects and would print out only the Link and Location as JSON or XML. Afterwards your javascript would parse the response and proceed to someothersite.com

What is interesting to me, though, is that the official jQuery ajax documentation page implies there is support for the 3xx redirects (near the documentation for the statusCode) but that doesn't seem to be working.

Community
  • 1
  • 1
tftd
  • 16,203
  • 11
  • 62
  • 106
  • I was wary to use success() since it's deprecated but even so, it looks like the Link header retrieved here still gets its data from the final destination with the 200 code and not the header from the response with the 3XX. – Mat Kelly Sep 22 '14 at 22:07
  • Probably because you're still doing the `302` redirect response. You should be returning a status code `200` and a custom header i.e. `MyRedirectHeader: http://someothersite.com` – tftd Sep 22 '14 at 22:10
  • Do you mean for me to return a custom header from the server? If so, per the question, "I have no control over the server code". If not, please clarify. Thanks. – Mat Kelly Sep 22 '14 at 22:14
  • Do you have any control over the server where your javascript is hosted? If so, you could create a php script there, which will serve the role of a proxy to `url: theURL` and in your ajax you would refer to the proxy script instead. If not, then I can't see another way to overcome this issue. – tftd Sep 22 '14 at 22:22
  • The JavaScript code resides in a Chrome extension and is accessing a service (hence no server-side control and no server aside from the one accessed), so a solution needs to be wholly JS-based. – Mat Kelly Sep 22 '14 at 22:26
  • I see. Well, at this point it appears there's nothing you can do to overcome the issue (http://stackoverflow.com/a/3820674/393805). By the way, I found out that Advanced Rest Client extension (http://is.gd/0Khi6R) was able to detect a 302 response and then proceed with the redirection. So I guess if you sniff their code, you might find out what you're looking for. – tftd Sep 22 '14 at 23:48