Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据get请求检查图像的状态_Javascript_Reactjs - Fatal编程技术网

Javascript 根据get请求检查图像的状态

Javascript 根据get请求检查图像的状态,javascript,reactjs,Javascript,Reactjs,我正在尝试检查get生成的图像的状态。 如果结果是404,我必须显示另一个图像, 图像链接始终存在,但有时图像正常,另一个返回404 我做错了什么?谢谢 已尝试: 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 t

我正在尝试检查get生成的图像的状态。 如果结果是404,我必须显示另一个图像, 图像链接始终存在,但有时图像正常,另一个返回404

我做错了什么?谢谢

已尝试:

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..
代码如下:

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");

});
同样的情况也发生在反应组分中。。甚至连形象都被打破了

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']} /> 
addDefaultSrc(ev){
ev.target.src=http://localhost/img/img-default.png'
}