[Vue.js的Axios Post的401未经授权的错误

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

我目前正在使用Axios Post将新用户添加到现有用户列表中。但是,我得到了未经授权的401错误。我正在使用Vue.js。如何克服未授权的错误?

这是我使用axios进行POST请求的代码。


export default {

  data(){
    return{
        posts:[],
        username:"",
     };
  },
methods:{

  create_user(){
     axios.post("http://******/*******",{
     username: this.username,
    })
    .then((response) => {
    const data = response.data;
    this.posts.push(data);
    });
  }
 }
}

如何修改我的发布请求代码以成功访问API并添加新用户?

post http-status-code-401
1个回答
-1
投票

您需要使用Authorization标头来做到这一点。

axios.post("http://******/*******", {
  username: this.username,
}, {
  headers: {
    'Authorization': 'jwt <jwt_token>'
  }
})
.then((response) => {
  const data = response.data;
  this.posts.push(data);
});
© www.soinside.com 2019 - 2024. All rights reserved.