【发布时间】:2018-10-24 22:11:28
【问题描述】:
在使用 JWT 令牌进行验证后,我正在从 nodejs API 获取图像。 我在浏览器中收到 GET 200 ok 响应网络标题和图片可以在预览中看到,但我不能在我的应用程序中使用它。
我肯定做错了什么。请让我知道从 API 显示图像的正确方法。 在我的后端 nodejs 上,我使用 res.sendFile 来发送文件。
class Card extends Component {
constructor({props, pic, token}) {
super(props, pic, token);
this.state = {
pic: pic,
};
urlFetch(data) {
fetch(data, {
headers: new Headers({
'authorization': `Bearer ${this.props.token}`,
'Content-Type': 'application/json'
})
})
.then(response => {
if (response.statusText === 'OK') {
return data // OR return response.url
}
})
}
render() {
const { pic } = this.state;
return (
<div>
<img style={{width: 175, height: 175}} className='tc br3' alt='none' src={ this.urlFetch(pic) } />
</div>
);
}
}
【问题讨论】:
-
src={ pic }应该可以工作。 -
@hurricane 它不会。获取的图像和状态属性
pic之间没有链接。urlFetch的回调中没有setState发生 -
@hurricane 正如我已经提到的,我在后端使用 JasonWebToken 来获取图像。
-
api返回图片url还是图片文件本身?
-
@roxxypoxxy 我可以在浏览器网络中看到图像 -> 预览。仅当后端授权成功时,我才会在我的 fetch 中返回图像 url。
标签: javascript node.js reactjs fetch-api