javascript - React 使用 fetch 从 api 获取图像
问题描述
我需要使用 fetch api 获取图像。我的同事说对服务器的调用应该返回一个base64字符串来显示图像。图片需要经过身份验证,我不能只使用它的网址。这就是我所拥有的:
fetch(`${apiUrl}filestore/${logo.name}`, {
.then(res => console.log(res)
我不太确定如何获取 base64 字符串。显然我没有使用正确的获取。这是控制台中显示的内容![屏幕截图][1]
解决方案
尝试将 res 格式化为 json。然后检查资源。
fetch(`${apiUrl}filestore/${logo.name}`, {
method: 'GET',
headers: {
Authorization: JSON.parse(localStorage.getItem('Hidden'))
? `Bearer ${JSON.parse(localStorage.getItem('Hidden')).access_token}`
: '',
},
})
.then(res => return res.json())
.then(res => console.log(res))
推荐阅读
- c# - C#/CIL:本机 int 类型
- c# - C# 检查对象是否为空
- c++ - CMake:无法打开共享对象文件:没有这样的文件或目录
- python - 为什么这两个使用 tkinter 创建小部件网格的程序执行不同?
- bash - 使用多个 if 语句生成命令所需的语法
- ruby-on-rails - Rails find_or_create_by 将强制转换添加到哈希值 json 类型属性?
- html - 量角器找不到 Angular 日期选择器
- elasticsearch - Elasticsearch top_hits 聚合与最新文档
- react-native - react-native 应用程序中某些屏幕的模态导航
- wpf - 如何从 DataGrid wpf 中的 ComboBox 列中获取 SelectedItem 属性