【发布时间】:2020-11-12 03:31:29
【问题描述】:
我正在关注有关构建 Laravel 和 Vue JS Web 应用程序的教程,我有一个问题:
我正在尝试使用 Laravel Passport 构建 api,但出现以下错误:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
app.js:699 Uncaught (in promise) Error: Request failed with status code 401
at createError (app.js:699)
at settle (app.js:960)
at XMLHttpRequest.handleLoad (app.js:168)
我只是想知道为什么会出现这些错误? 我已经运行了以下命令: '作曲家需要 laravel/护照' php工匠迁移 php工匠护照安装
用户.php
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
配置/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
routes.php
Route::post('/categories/upsert', 'CategoryController@upsert');
index.blade.php
<template>
<form>
<a @click="addCategory" class="add">+ Add Category</a>
<div v-for="(category, index) in categories" :key="category.id">
<input type="text" v-model="category.name"> {{/* inputs will use two way binding with v-model */}}
<input type="number" v-model="category.display_order">
<a @click="removeCategory(index)" class="remove">delete</a>
<div>
<img v-if="category.image" :src="` /images/${category.image}`" width="100">
<label v-else>Image: </label>
<input type="text" v-model.lazy="category.image">
</div>
<hr>
</div>
</form>
</template>
<script>
export default {
props: ['initialCategories'],
data() {
return {
categories: _.cloneDeep(this.initialCategories) /* Cloned to avoid mutating the prop */
};
},
created() {
axios.post('/api/categories/upsert');
},
methods: {
removeCategory(index) {
if (confirm('Are you sure?')){
this.categories.splice(index, 1);
}
},
addCategory() {
this.categories.push({
id: 0,
name: '',
image: '',
display_order: this.categories.length + 1
});
this.nextTick(() => {
window.scrollTo(0, document.body.scrollHeight);
this.$refs[''][0].focus();
});
}
}
}
</script>
如果有人有任何想法,请告诉我。我尝试清除配置缓存并包含 csrf 令牌。谢谢,
罗伯特 英国伦敦
【问题讨论】:
-
我已经尝试过了,我已经将它添加到我的 AppServiceProvider 中的“boot()”函数中: public function boot() { Schema::defaultStringLength(191); \Laravel\Passport\Passport::withoutCookieSerialization();但我仍然收到相同的 401 错误:(
标签: php html laravel axios laravel-passport