如何配置 Laravel CORS 接受 Nuxt 3 代理 HTTP 请求?
P粉739886290
P粉739886290 2023-12-24 19:42:58
[Vue.js讨论组]

我正在尝试将客户端请求从 Nuxt 3 发送到 api 路由,该路由通向 Laravel 中的身份验证控制器,但它返回:

Access to fetch at 'http://127.0.0.1:8000/api/authenticate' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

请求由 Nuxt 3 的 $fetch api 发送:

$fetch('http://127.0.0.1:8000/api/authenticate', {
        method: 'POST',
        body: form
    }).then((res) => {console.log(res)})

请求似乎在到达控制器之前就被停止了。这是routes/api.php

// works
Route::get('/test', function () {
    return 'hello laravel';
});

// doesn't work, throws CORS error before request can reach Controller
Route::post('/authenticate', [AuthenticatedSessionController::class, 'test']);

// works
Route::post('/authenticate', function () {
    return 'works';
});

Laravel 9.2 开始,似乎有一个 config/cors.php 文件可以配置,但我不知道如何配置。默认情况如下所示:

 ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];

知道如何允许来自 Nuxt 3 的 api 请求auth 路由 吗?

P粉739886290
P粉739886290

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号