5

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']} /> 
Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
larry_82
  • 319
  • 1
  • 4
  • 15
  • 4
    Possible duplicate of [react.js Replace img src onerror](https://stackoverflow.com/questions/34097560/react-js-replace-img-src-onerror) – bryan60 May 09 '19 at 15:31

0 Answers0