【问题标题】:Laravel subfolder - axios post not workingLaravel 子文件夹 - axios 帖子不起作用
【发布时间】:2019-01-26 13:19:45
【问题描述】:

我正在尝试使用 Laravel 和 Vue 在子文件夹中使用 jwt 构建 SPA,但由于某种原因,我在加载页面时收到 405 Method Not Allowed。这是代码:

helper.js 中的方法

export default {
    check() {
        return axios.post('api/auth/check').then(response =>  {
            return !!response.data.authenticated;
        }).catch(error => {
            return response.data.authenticated;
        });
    }
}

Vue 路线

router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requiresAuth)){
        return helper.check().then(response => {
            if (!response){
                return next({ path : '/unikit/login'});
            }

            return next();
        });
    }

    if (to.matched.some(m => m.meta.requiresGuest)) {
        return helper.check().then(response => {
            if (response) {
                return next({ path : '/unikit'});
            }

            return next();
        });
    }

    return next();
});

export default router;

api中的路由:

Route::group(['prefix' => 'auth'], function () {
    Route::post('/unikit/check','AuthController@check');
});

控制器方法:

public function check()
{
    try {
        JWTAuth::parseToken()->authenticate();
    } catch (JWTException $e) {
        return response(['authenticated' => false]);
    }

    return response(['authenticated' => true]);
}

还尝试在路由中删除“unikit”目录和斜杠,但没有任何反应。

【问题讨论】:

  • 您正在向“api/auth/check”发出 ajax 请求,但您在 api 中显示的路线是“api/auth/unikit/check”。由于您收到 405 错误,因此 api/auth/check 必须存在,也许作为“get”方法?

标签: laravel vue.js single-page-application


【解决方案1】:

经过两个小时的思考,我找到了解决方案。 Provider 中 api 路由的前缀需要是 修改为子文件夹。 Route::prefix('/subfolder/api') 问题解决了!感谢大家的帮助!

【讨论】:

    【解决方案2】:

    看起来像路线问题。方法无法获取路由:

    {message: "", exception: "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",…}
    exception
    :
    "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"
    file
    :
    "C:\xampp\htdocs\unikit\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php"
    line
    :
    255
    message
    :
    ""
    trace
    :
    [,…]
    

    路线列表:

    +--------+----------+----------------+------+-------------------------------------------+------------+
    | Domain | Method   | URI            | Name | Action                                    | Middleware |
    +--------+----------+----------------+------+-------------------------------------------+------------+
    |        | POST     | api/auth/check |      | App\Http\Controllers\AuthController@check | api        |
    |        | GET|HEAD | {vue?}         | home | Closure                                   | web        |
    +--------+----------+----------------+------+-------------------------------------------+------------+
    

    【讨论】:

      【解决方案3】:

      你的路线是:/unikit/check

      Route::post('/unikit/check','AuthController@check');
      

      但您正在发布到 /check

      axios.post('api/auth/check')
      

      把这个^^改成:

      axios.post('api/auth/unikit/check')
      

      【讨论】:

      • 您 100% 确定您以正确的格式发布到正确的链接?
      猜你喜欢
      • 2018-03-01
      • 2017-07-27
      • 1970-01-01
      • 2014-09-09
      • 2018-10-10
      • 2013-01-11
      • 1970-01-01
      • 2021-09-10
      • 2016-07-18
      相关资源
      最近更新 更多