我想使用 axios 在 React 中插入带有图像的数据

问题描述 投票:0回答:1

我想使用 axios 在 React 中插入带有图像的数据,但我遇到了问题

const useInsertDataWithImage = async (url, params) => {
  const config = {
    headers: { 'Content-Type': 'multipart/form-data' },
    Authorization: `Bearer ${localStorage.getItem('token')}`,
  };

  const res = await baseURL.post(url, params, config);
  console.log(res);
  return res.data;
};

这是我的功能 令牌是有效的,它给了我

状态:“未找到授权令牌” 请注意它在邮递员中工作

我尝试使用参数合并 url 以仅传递配置,但我做不到

javascript reactjs axios
1个回答
0
投票

你的

config
是如何用在
post
方法中的?典型的,在
fetch
axios
中,
Authorization
标头位于
header
对象内部,如下所示:

const config = {
    headers: {
        'Content-Type': 'multipart/form-data',
         Authorization: `Bearer ${localStorage.getItem('token')}`
    },
};

如果您已正确添加

Authorization
标头,请仔细检查您的令牌是否存在。顺便说一句,当您通过
Content-Type
上传图像时,通常会自动添加
FormData
标头。

© www.soinside.com 2019 - 2024. All rights reserved.