Test for a 404 on a remote server


#1

I want to display a certain image if it exists on a remote server. Otherwise, I want to show a different image. So I need a way of testing if an image exists on a remote server. I’m trying the following code, but it fails every time, even if the image is really there.

let observable = this.http.head(imageURL);
observable.subscribe(()=>{
  console.log("Success! :)");
}, (error)=>{
  console.log("Error :(");
}

The error message is something like “No ‘Access-Control-Allow-Origin’ header is present on the requested resource.” But this doesn’t make sense, because I can display the image just fine in my Ionic app if I choose to.

Any ideas?


#2

Where does this come from?

What do you mean by that? “display” how?


#3

this.http comes from import { Http } from ‘@angular/http’;

By “display the image” I mean I can put an tag in my html and I can see the image there when I run the app.


#4

Downloading a file and showing it are probably two different things from a security standpoint, which causes the CORS logic to trigger which requires this header on your server.