laravel解决跨域

最新配置与以前不一样了,参考github:

1、composer require barryvdh/laravel-cors 
    //github搜搜 barryvdh/laravel-cors 

2、在config/app.php中的provider中配置:
    Barryvdh\Cors\ServiceProvider::class

3、配置跨域
    3.1 配置一个api组都允许跨域
    在/app/Http/Kernel.php中的$middlewareGroups的api中
    添加:\Barryvdh\Cors\HandleCors::class,这样整个api组
    都允许跨域
    路由:
    Route::get('/xxx', function () {
        return 'xxx';
    });  //默认所有api下都已经配置了

    3.2 配置单个路由
    在/app/Http/Kernel.php中的$$routeMiddleware中添加
    'cors'=>\Barryvdh\Cors\HandleCors::class
    路由:
    Route::middleware('cors')->get
    ('/xxx', function () {
        return 'xxx';
    });  //单个路由配置跨域

4、生成跨域配置文件config/cors.php:
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"   

猜你喜欢

转载自blog.csdn.net/jj546630576/article/details/79253214