我目前正在使用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并添加新用户?
您需要使用Authorization
标头来做到这一点。
axios.post("http://******/*******", {
username: this.username,
}, {
headers: {
'Authorization': 'jwt <jwt_token>'
}
})
.then((response) => {
const data = response.data;
this.posts.push(data);
});