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