Basically, I need to check if the URL of an image is working so I can show it or show some placeholder image instead.
The best solution seems to make an Ajax request or something similar and do the handling from there depending of the response. The problem is that request says that it 'has been blocked by CORS', so this always returns false, so the function always show the placeholder image.
function checkImg(url){
$.ajax({
url: url,
method: 'GET',
dataType: 'json'
}).done(function (response) {
return true
}).fail(function(jqXHR, textStatus, errorThrown) {
return false
})
}
if checkImg(url){
# do something
} else {
# do something else
}
What annoys and confuses me the most is the fact that I can access the images directly (from this URL, for example: https://vikua.com/images_cameras_rails/521474/large/image works when clicked) but the console returns the 'blocked by CORS' error when doing a request.
Also, the image also works if I put the link directly in an HTML tag, but I can't do error handling from there, can I?
I appreciate any answers.
Edit: It turns out I actually can handle this kind of error with a bit of HTML. Using 'onerror' worked! Thanks a lot to jonatjano for the solution!