4

I have a problem.. in my project, i need to make an AJAX request to a URL shortener site (such as bit.ly, etc..).

My target is either I could get the redirect URL or the content of the redirected site. This far, after I tried some techniques from the web, I still cannot get the correct result.

I tried to catch the 301 HTTP response status (the default behaviour of URL shortener sites), but I found that the request never returns 301. Strangely, it returns 200.

Any idea how to solve this? Thanks a lot.. :)

Abou
  • 65
  • 2
  • 6
  • The 200 is because the request automatically follows the 301. I don't know whether that can be turned off – Pekka Oct 06 '11 at 09:02

1 Answers1

4

According to the spec quoted in this answer a 30x header will automatically be followed and there is no way to get hold of the redirecting resource from within an Ajax call:

The same-origin request event rules are as follows:

If the response has an HTTP status code of 301, 302, 303, or 307 If the redirect violates infinite loop precautions this is a network error.

Otherwise, run these steps:

  • Set the request URL to the URL conveyed by the Location header.

  • If the XMLHttpRequest origin and the origin of request URL are same origin transparently follow the redirect while observing the same-origin request event rules.

  • Otherwise, follow the cross-origin request steps and terminate the steps for this algorithm.

You will probably need to use a server-side solution for this.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks for the explanation... yes, I think server-side scripting will help too... But, unfortunately in my project, i can't use any server side scripting... only client-side scripting allowed.. :( – Abou Oct 06 '11 at 09:54
  • @Abou Maybe somebody manages to come up with some genius way I can't see, but I don't think you can do this on the client side alone. – Pekka Oct 06 '11 at 12:03