I am building an AngularJS web app and at the moment I am creating a form to allow the user to link to youtube videos. I have a thumbnail associated with youtube videos and when the user enters a youtube link, i fetch the image from https://img.youtube.com/vi/${videoId}/maxresdefault.jpg
That goes into this:
<img src="{{element.video.thumb}}>
So, my current solution is:
element.video.thumb = `https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`
My problem is that not all youtube videos have their thumbnail available at that link. I want to be able to identify if a video has it available or not. I notice that I get a 404 response in my console when a thumbnail is not available. So I tried to check if the thumbnail exists using $http service. I tried both variants below:
$http.get(`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`)
$http.jsonp(`https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`,{jsonpCallbackparam: "callback"})
The first one gives me a CORS error, and the second one doesn't work because the MIME type of the response is image/jpeg
and it gives error when trying to execute the callback.
My first question is:
Why assigning the link to the src attribute of an img fetches the resource with no issues, but trying to access the resource with ah HTTP request gives a CORS error?
My second question is:
How can I execute some code if I get a 404 response after assigning the img src attribute?