You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 11配置config/cors.php后仍存在CORS问题求助

Laravel/React Vite项目CORS问题求助

开发Laravel/React Vite项目时遇到CORS问题,按Laravel官方文档说明在config目录下创建了cors.php配置文件,但前端仍出现CORS报错。尝试过fruitcake等第三方CORS包解决方案,因依赖冲突无法安装。以下是相关配置与代码:

config/cors.php配置

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => false,

    'max_age' => 0,

    'supports_credentials' => true,

];

前端Axios请求代码

useEffect(() => {
    const getReportList = async () => {
        try {
            setIsLoading(true);
            const response = await API.get('admin/reports');
            setReports(response.data.data);
            setFilteredReports(response.data.data);
            setActiveReport(response.data.data[0]);
        } catch (error) {
            errorManager(error);
        } finally {
            setIsLoading(false);
        }
    }
    getReportList();
}, [])

Axios实例代码

import axios from "axios";
import {
    SERVER_URL
} from "../../constants";

const API = axios.create({
    baseURL: SERVER_URL[
        import.meta.env.VITE_ENVIRONMENT],
    timeout: 12000,
    withCredentials: true
});

API.interceptors.request.use(async (config) => {
    return config;  //to handle later
});

API.interceptors.response.use(
    function (response) {
        return response;  //to handle later
    },
    function (error) {
        return error; //to handle later
    }
);

export default API;

前端CORS报错截图

前端CORS报错截图

内容的提问来源于stack exchange,提问作者ALLAOUA Okba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:36:09