I'm trying to check the status of an image resulting from a get. if the result is a 404 I must show another image, The image links always exist but sometimes the image is ok an another return 404..
what am I doing wrong? Thanks!
already try:
if(data['img_info'] == 404) // always returns false even the image link is broke.
also whit data['img_info'].naturalWidth === 0 // whit the same result..
finaly i use the catch..
here is the code:
fetch('url')
.then(function (response) {
return response.json();
}).then(function (data) {
const img_info = data['img-info']
$('#content').css({'background-image' : 'url(' + img_info + ')','background-size': 'cover'})
}).catch(function () {
console.log('error')
const img_default = 'http://localhost/img/img-default.png'
$('#content').css({'background-image' : 'url(' + img_default + ')','background-size': 'cover' })
console.log("Error");
});
the same happens in the reacting component.. even the image is broke
addDefaultSrc(ev){
ev.target.src = 'http://localhost/img/img-default.png'
}
<img onError={this.addDefaultSrc} className="img-slider img-responsive" src={this.props.program['img-info']} />