共计 916 个字符,预计需要花费 3 分钟才能阅读完成。
laravel 的接口有时候会遇到跨域问题,报错:
blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed
查阅资料,网上说的各种中间件,添加 header 方法等等的,均不起任何作用。
后来发现配置文件有一个 cors.php 的配置,进去查阅:
/*
|--------------------------------------------------------------------------
| 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
|
*/
基本可以判断,这里就是控制跨域了,下面第一行,有个 path 的参数, 只需要将你的 url 参数加进来就可以了。
'paths' => ['api/*','sanctum/csrf-cookie'], // 这里增加路由参数
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
正文完
加入官方交流QQ群:778957856