4

I want to write a JavaScript or jQuery method to send a URL name as a post request and get the changed URL [more appropriately, REDIRECTED URL].

For example, if I append a random integer to the end of facebook.com, like http://facebook.com/940, the URL gets redirected to http://www.facebook.com/melissa.miller.967 My requirement is to get this new URL. Can someone assist me in this regard

Shobhit
  • 773
  • 7
  • 20
  • I don't think it's the DNS updating it to that. I think the URL is getting redirected. – crush Jun 03 '13 at 14:08
  • 1
    possible duplicate of [How can I get the redirect url using javascript?](http://stackoverflow.com/questions/11698178/how-can-i-get-the-redirect-url-using-javascript) – Jeremy Jun 03 '13 at 14:14
  • 4
    You can't do this entirely in JavaScript; browser domain security prevents you from seeing the response (in this case, the redirect) that is sent back from a page on a different domain. – Jeremy Jun 03 '13 at 14:14
  • @crush Sorry for the choice of wrong words...modified the question. Thanks for pointing it out. – Shobhit Jun 03 '13 at 14:16
  • @JeremyBanks You can assume that the URL under consideration will get redirected to an URL of same domain. Bytheway, checking out the duplicate question pointed out by you. Thanks. – Shobhit Jun 03 '13 at 14:32
  • Does it have to be in JavaScript? If you can use PHP I think you may be able to use cURL and get the information from the redirect header. See http://stackoverflow.com/questions/4062819/curl-get-redirect-url-to-a-variable – Celeritas Jun 04 '13 at 07:01

1 Answers1

0
$.post('/path/to/file', {url: short_url}, function(data) {
   window.location = data.url;
 });
David
  • 1,829
  • 20
  • 31
  • Tried $.ajax({ type: "POST", url: 'http://www.facebook.com/940', success: function(data) { console.log('gotIt'); }, dataType: 'default' }); It returns the following errors (1).OPTIONS http://www.facebook.com/940 404 (Not Found) (2)OPTIONS http://www.facebook.com/940 Origin null is not allowed by Access-Control-Allow-Origin. (3).XMLHttpRequest cannot load http://www.facebook.com/940. Origin null is not allowed by Access-Control-Allow-Origin. However, if I specify http://www.facebook.com/melissa.miller.967 as the URL, no errors are thrown. It seems it is not getting redirected in first case. – Shobhit Jun 06 '13 at 08:10