【发布时间】:2018-11-17 20:29:32
【问题描述】:
我正在开发一个带有单独的 Laravel 后端 API 的 Vue 应用程序。后端有 Laravel 护照,在进行数据库调用时需要访问令牌。
通常一切正常,我可以从数据库中取回数据,但由于某种原因,我的 2 个调用出错,即 POST en PUT。我不知道为什么我从 laravel 护照返回未经身份验证(401),而我的 get 请求进展顺利。此外,POST 和 PUT 在邮递员应用程序中运行良好。
get 请求
getSuppliers() {
axios.get(`${this.$API_URL}/api/v1/relations`, {
headers: this.headers,
})
.then((response) => {
this.loaded = true;
this.relations = response.data.data;
})
.catch(error => console.log(error));
},
post 请求
axios.post(`${this.$API_URL}/api/v1/relations`, {
headers: this.headers,
data: {
company_name: this.supplier.company_name,
language_id: this.supplier.language_id,
supplier_type_id: this.supplier.supplier_type_id,
email: this.supplier.email,
website: this.supplier.website,
recognition_number: this.supplier.recognition_number,
street: this.supplier.street,
house_number: this.supplier.house_number,
zipcode: this.supplier.zipcode,
city: this.supplier.city,
country: this.supplier.country,
},
})
.then((response) => {
console.log(response);
// retrieve the id
// push the user to the show of the retrieved id
})
.catch(error => console.log(error));
访问令牌的辅助函数
function getHeaders(token) {
return {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
};
}
function getToken() {
const oauth = JSON.parse(localStorage.getItem('oauth') || '{}');
if (oauth.access_token) {
return oauth.access_token;
}
return false;
}
有人遇到同样的问题或类似问题吗?
【问题讨论】:
标签: laravel vue.js axios laravel-passport