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 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

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

4年前 评论
Talentisan (楼主) 4年前
Talentisan (楼主) 4年前
Talentisan (楼主) 4年前
jiangtaigong 4年前
ynwa 1年前
讨论数量: 16

发下配置

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

<?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,

];

4年前 评论

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

4年前 评论
Talentisan (楼主) 4年前
Talentisan (楼主) 4年前
Talentisan (楼主) 4年前
jiangtaigong 4年前
ynwa 1年前
'supports_credentials' => false

改为 true

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

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

4年前 评论
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,
4年前 评论

遇到了同样的问题

4年前 评论

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