【问题标题】:Getting error 401 while fetching data with axios in Laravel在 Laravel 中使用 axios 获取数据时出现错误 401
【发布时间】:2020-12-07 01:11:17
【问题描述】:

我正在尝试在 Laravel 中使用 axios 获取一些 报告。我在 Postman 中测试过,一切都很好。 但在这里我得到了错误 401。 这是我的代码:

#Reports.vue 文件

<template>
    <div>
        <h1>All reports: </h1>
        <div v-for="report in reports" v-bind:key="report.id">
            <p>{{ report.description }}</p>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Reports",
        data ()  {
            return  {
                reports : [],
            }
        },
        mounted() {
            this.fetchReports();
        },
        methods : {
            async fetchReports() {
                const {data} = await axios.get('/api/report');
                this.reports = data.data;
            },
        }
    }
</script>

#ReportController@index

public function index()
{
    $reports = Report::all();

    return ReportResource::collection($reports);
}

#api.php

Route::group(['middleware' => 'auth:api'], function () {    
  Route::apiResource('/report', 'API\ReportController');
});

提前致谢。如果您需要更多信息,我会发布。

【问题讨论】:

    标签: laravel vue.js http axios


    【解决方案1】:

    Laravel auth:api 中间件要求您在请求中传递 Bearer 令牌或 ?api_token= url 参数

    Source

    我没有看到您通过 axios 请求传递令牌

    【讨论】:

    • 我正确传递了令牌。我通过 Postman 对其进行了测试……我传递它的逻辑是在 AuthController 中……我不确定我在这里犯了什么错误……如果我今天发现,我会发布错误的内容。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2021-11-05
    • 2017-03-03
    相关资源
    最近更新 更多