POST出现401错误,并放置Laravel passport/Vue/Axios
我正在使用一个单独的Laravel后端API开发一个Vue应用程序。后端具有Laravel passport,在执行数据库调用时需要访问令牌 正常情况下,一切正常,我可以从数据库中获取数据,但由于某种原因,我的两个调用出现错误。我不知道为什么当我的POST出现401错误,并放置Laravel passport/Vue/Axios,laravel,vue.js,axios,laravel-passport,Laravel,Vue.js,Axios,Laravel Passport,我正在使用一个单独的Laravel后端API开发一个Vue应用程序。后端具有Laravel passport,在执行数据库调用时需要访问令牌 正常情况下,一切正常,我可以从数据库中获取数据,但由于某种原因,我的两个调用出现错误。我不知道为什么当我的get请求进展顺利时,我从laravel passport获得了未经身份验证的(401)护照。此外,POST和PUT在postman应用程序中都运行良好 get请求 getSuppliers() { axios.get(`${th
get
请求进展顺利时,我从laravel passport获得了未经身份验证的(401)护照。此外,POST和PUT在postman应用程序中都运行良好
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));
},
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));
post
请求
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));
},
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;
}
有人有同样的问题或相似的问题 经过一番挖掘,浏览了我的其他代码并请求,我找到了一个解决方案,为我解决了这个问题 而不是
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));
我不得不这么做
axios({
method: 'post',
url: `${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));
除了样式之外,我看不到任何区别,所以我不知道为什么第二种样式有效,而第一种不有效,特别是对于
put
和post
类型的请求。同样,这怎么可能是一个逻辑解释?速记电话不起作用,但普通电话起作用。。。。。。