fruitcake/Laravel-cors 如何正确使用呢

Access to XMLHttpRequest at ‘jianzhi.com/api/video_v2/login' from origin ‘http://192.168.20.7:8081' has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

我已经按照:博客:laravle7.0 Laravel-cors 开发包实现跨域 这篇文章配置了好几遍 依旧错误

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

确保不要有 exit,die,dd 之类。

3年前 评论
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前
jiangtaigong 3年前
ynwa 8个月前
讨论数量: 16

发下配置

3年前 评论
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前

<?php

return [

/*
|--------------------------------------------------------------------------
| Laravel CORS Options
|--------------------------------------------------------------------------
|
| The allowed_methods and allowed_headers options are case-insensitive.
|
| You don't need to provide both allowed_origins and allowed_origins_patterns.
| If one of the strings passed matches, it is considered a valid origin.
|
| If array('*') is provided to allowed_methods, allowed_origins or allowed_headers
| all methods / origins / headers are allowed.
|
*/

/*
 * You can enable CORS for 1 or multiple paths.
 * Example: ['api/*']
 */
'paths' => ['api/*'],

/*
* Matches the request method. `[*]` allows all methods.
*/
'allowed_methods' => ['*'],

/*
 * Matches the request origin. `[*]` allows all origins. Wildcards can be used, eg `*.mydomain.com`
 */
'allowed_origins' => ['*'],

/*
 * Patterns that can be used with `preg_match` to match the origin.
 */
'allowed_origins_patterns' => [],

/*
 * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
 */
'allowed_headers' => ['*'],

/*
 * Sets the Access-Control-Expose-Headers response header with these headers.
 */
'exposed_headers' => [],

/*
 * Sets the Access-Control-Max-Age response header when > 0.
 */
'max_age' => 0,

/*
 * Sets the Access-Control-Allow-Credentials header.
 */
'supports_credentials' => false,

];

3年前 评论

确保不要有 exit,die,dd 之类。

3年前 评论
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前
Talentisan (楼主) 3年前
jiangtaigong 3年前
ynwa 8个月前
'supports_credentials' => false

改为 true

3年前 评论
Talentisan (楼主) 3年前
wanghan

HandleCors::class,要放到全局中间键的第一个

3年前 评论
wanghan
/**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class, 
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

config文件中

/*
     * Sets the Access-Control-Allow-Credentials header.
     */
    'supports_credentials' => true,
3年前 评论

遇到了同样的问题

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!