我正在通过使用Vue js以及vue axios和vue路由器在laravel中创建一个简单的CRUD操作。 路由工作正常,但问题是当我使用axios保存数据时,得到的响应是401未经授权。 我已经在web.php中保护了我的路线
Route::get('/{any}', function () {
return view('posts');
})->where('any', '.*')->middleware(['auth']);
在api.php中,我有这样的路由。
Route::post('/quiz/create-quiz-category', 'QuizCategoryController@store')->middleware(['auth']);
我当前正在使用laravel默认身份验证系统。 下面是我的Vue组件脚本代码。
this.axios.post(uri, {post: this.post}, {
headers: {
'x-csrf-token': document.querySelectorAll('meta[name=csrf-token]')[0].getAttributeNode('content').value,
'Accept' : 'application/json'
}
}).then((response) => {
// this.$router.push({name: 'home'});
});
如果有人告诉我代码有什么问题,并且我目前没有使用任何护照或jwt,我将不胜感激。
谢谢。
答案 0 :(得分:0)
api_token:this.user.api_token
axios.post("http://foo",
{headers: { 'Authorization' : 'Bearer '+ api_token}})
.then(response => {
//action
})
链接:https://forum.vuejs.org/t/401-unauthorized-vuejs-laravel-and-passport/59770
或
postComment() {
axios.post('/api/posts/'+this.post.id+'/comment', {
api_token: this.user.api_token,
body: this.commentBox
})
但请确保您具有“ user.api_token”