0

In our AngularJS app I have an image URL like:

http://example.com/images/get/785746378

The issue is this redirects me to another URL which blocked:

http://another-example.com/images/f/gs/s/your-image.jpg

So the image is not working, but if I replace the domain so it's:

http://example.com/images/f/gs/s/your-image.jpg

It works fine.

So I want to be able to ping the URL in JavaScript and find out what the redirected URL is and change the domain so it works before adding it to my img src

Holly
  • 7,462
  • 23
  • 86
  • 140

2 Answers2

0

This should work fine:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(response) {
  var xhr = response.target;
  if (xhr.readyState === 4 && xhr.status === 302) {
    var redirectURL = xhr.location;
    var imageURL = redirectURL.replace('http://another-example','http://example');
    // change your image src here by imageURL
  }
}
xhr.open('GET', 'http://example.com/images/get/785746378', true);
xhr.send(null);
Alexandre Nicolas
  • 1,851
  • 17
  • 19
0

Its not clear from your post how you are requesting the image. Assuming that you are requesting it using AJAX / $HTTP:

You can send HEAD request for original URL. If HTTP HEAD requests are allowed on the server, you will get the HTTP 302 response with new location in the response header. You can do the required manipulation on this location and send GET request for the new URL of the image.

Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32