繁体   English   中英

React-Axios 获取 PUT 方法的错误 401(未经授权)

[英]React-Axios Getting error 401 (Unauthorized) for PUT Method

我浏览了很多帖子并测试了其中的大部分,但我仍然遇到此错误。 有趣的是,我没有为 get 方法收到此错误。 我只为 put 方法收到此错误。 谢谢大家

axios 错误 401(未授权)

axios
      .put("/api/v1/Profile", {
        fullName: userName,
        gender: Number(userGender),
        phoneNumber: userNumber,
        headers: {
          Accept: 'application/json',
          "Content-Type": "application/json; charset=UTF-8",
          Authorization: `Token ${userToken}`,
        },
      })
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      });

get 方法没有错误,但它给出了我用 Postman 测试的 put 方法的错误并且它没有错误

您的Authorization标头有问题。 您使用的是正确的语法( 请参阅 MDN ):

Authorization: <auth-scheme> <authorization-parameters>

鉴于userToken是您的<authorization-parameters> 问题来自您使用Token<auth-scheme> :根据IANA 维护的列表,这不是有效的方案。

没有太多信息很难确定,但我想你想要的是:

Authorization: `Bearer ${userToken}`,

我的问题用这种格式解决了:

axios({
  method: 'put',
  url: 'api/v1/Profile/',
  data: {
    fullName: userName,
    gender: Number(userGender),
    phoneNumber: '',
  },
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
    Authorization: `Bearer ${userToken}`,
  },
}).then((response) => {
  console.log(response);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM