Reputation: 65
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.. :)
Upvotes: 4
Views: 5599
Reputation: 449415
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.
Upvotes: 4