Php 向laravel服务器发送POST请求时获取{quot;消息::“未经身份验证”
我在从Vue axios向带有sanctum的laravel服务器发送POST请求时遇到问题 首先我要拿到代币 我的Php 向laravel服务器发送POST请求时获取{quot;消息::“未经身份验证”,php,laravel,vue.js,axios,Php,Laravel,Vue.js,Axios,我在从Vue axios向带有sanctum的laravel服务器发送POST请求时遇到问题 首先我要拿到代币 我的api.php路径 Route::post('/get_token', function (Request $request) { $request->validate([ 'email' => 'required|email', 'password' => 'required', 'device_name'
api.php路径
Route::post('/get_token', function (Request $request) {
$request->validate([
'email' => 'required|email',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
return $user->createToken($request->device_name)->plainTextToken;
});
Route::middleware('auth:sanctum')->get('/book/all', 'BookController@all');
Route::middleware('auth:sanctum')->post('/book/add', 'BookController@add');
vue axios
this.$axios.post('/api/get_token', {
email: this.email,
password: this.password,
device_name: this.device_name,
}).then(response=>{...});
this.$axios.get('/api/book/all', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken
}
}).then(response=>{...})
this.$axios.post('/api/book/add', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
},
data: {
title: this.addingTitle,
author: this.addingAuthor,
}
})
当我用收到的令牌发送GET请求时,它工作正常
vue axios
this.$axios.post('/api/get_token', {
email: this.email,
password: this.password,
device_name: this.device_name,
}).then(response=>{...});
this.$axios.get('/api/book/all', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken
}
}).then(response=>{...})
this.$axios.post('/api/book/add', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
},
data: {
title: this.addingTitle,
author: this.addingAuthor,
}
})
api.php
Route::post('/get_token', function (Request $request) {
$request->validate([
'email' => 'required|email',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
return $user->createToken($request->device_name)->plainTextToken;
});
Route::middleware('auth:sanctum')->get('/book/all', 'BookController@all');
Route::middleware('auth:sanctum')->post('/book/add', 'BookController@add');
但是当我尝试发送POST请求时,我得到了{“message”:“Unauthenticated.”}
和401错误
vue axios
this.$axios.post('/api/get_token', {
email: this.email,
password: this.password,
device_name: this.device_name,
}).then(response=>{...});
this.$axios.get('/api/book/all', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken
}
}).then(response=>{...})
this.$axios.post('/api/book/add', {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
},
data: {
title: this.addingTitle,
author: this.addingAuthor,
}
})
api.php
Route::post('/get_token', function (Request $request) {
$request->validate([
'email' => 'required|email',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
return $user->createToken($request->device_name)->plainTextToken;
});
Route::middleware('auth:sanctum')->get('/book/all', 'BookController@all');
Route::middleware('auth:sanctum')->post('/book/add', 'BookController@add');
对不起,我只是一个网络迷,不懂很多东西,希望你能帮助我。如果你使用
post
,那么第一个参数是URL,第二个参数是你发布的数据。在您的情况下,使用以下选项可能更有意义:
this.$axios({
method: 'POST',
url: '/api/book/add'
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
},
data: {
title: this.addingTitle,
author: this.addingAuthor,
}
})
或者,如果仍要使用.post
,则:
this.$axios.post('/api/book/add', {
title: this.addingTitle,
author: this.addingAuthor,
}, {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
}
})
如果使用
post
,则第一个参数是URL,第二个参数是您正在发布的数据。在您的情况下,使用以下选项可能更有意义:
this.$axios({
method: 'POST',
url: '/api/book/add'
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
},
data: {
title: this.addingTitle,
author: this.addingAuthor,
}
})
或者,如果仍要使用.post
,则:
this.$axios.post('/api/book/add', {
title: this.addingTitle,
author: this.addingAuthor,
}, {
headers: {
"Authorization": 'Bearer ' + this.$store.getters.getToken,
}
})
只有
book/add
似乎需要根据您共享的内容进行授权,因此book/all
有效的事实并不表明这是其他方面的问题route@apokryfos是的,问题可能不仅存在于book/add
,它只是我项目中唯一的post请求;)如果我尝试在没有令牌的情况下将get请求发送到book/all
,我也会收到{“message”:“Unauthenticated.”。抱歉,英语不好,book/add
似乎需要基于您共享的内容进行授权,因此book/all
有效这一事实并不意味着这只是另一个问题route@apokryfos是的,问题可能不仅存在于book/add
,它只是我项目中唯一的post请求;)如果我尝试在没有令牌的情况下将get请求发送到book/all
,我也会收到{“message”:“Unauthenticated.”。对不起,我的英语不好。非常感谢,我没想到,问题就在前面。非常感谢,我没想到,问题就在前面