Laravel子文件夹-Axios发布不起作用

时间:2018-08-20 11:58:41

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

我正在尝试在子文件夹中使用jwt用Laravel和Vue构建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”目录和斜线,但什么也没发生。

3 个答案:

答案 0 :(得分:1)

经过两个小时的思考,我找到了解决方案。提供者中api路由的前缀必须为 修改为子文件夹。 lens = [len(a) for a in states] ,问题解决了!谢谢大家的帮助!

答案 1 :(得分:0)

您的路线是: / unikit / check

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

但是您要发布到 /检查

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

将此^^更改为:

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

答案 2 :(得分:0)

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

{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        |
+--------+----------+----------------+------+-------------------------------------------+------------+