使用 Socket.io 和 Redis 在 Laravel Echo 服务器上缺少 CORS 允许来源

CORS Missing Allow Origin on Laravel Echo Server with Socket.io and Redis

您好,我在 laravel 应用程序的视图页面上添加后出现以下错误。

我正在使用 Windows 10 .

缺少允许来源的 CORS

CORS Missing Allow Origin Error Screenshot

以下步骤将解决您的 Echo 服务器的“CORS Missing Allow Origin”错误

第 1 步: 添加“传输:['websocket'、'polling'、'flashsocket'] // 修复 CORS 错误!” Echo 服务器设置文件 (public\js\laravel-echo-setup.js) 中的代码如下所示:

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    transports: ['websocket', 'polling', 'flashsocket'] // Fix CORS error!
});

第 2 步: 使用“npm 运行 dev”命令构建您的代码 使用“laravel-echo-server start”命令重新启动 Laravel Echo 服务器 现在检查您的应用程序,您现在找不到 CORS 错误。