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 开发包实现跨域 这篇文章配置了好几遍 依旧错误

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 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年前 评论

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