传参就会跨域。

laravel想解决跨域问题,需要动好几个页面。

thinkPHP6就简单多了。

针对某个域名,取消单个路由的跨域限制

Route::get('/xxx/xxx/:id', 'xxxx')->allowCrossDomain([
    'Access-Control-Allow-Origin'        => 'http://域名地址',
    'Access-Control-Allow-Credentials'   => 'true'
]);

针对某个域名,取消某个组路由的跨域限制

Route::group('/h5', function(){
    Route::get('/xxx/xxx/:id', 'xxxx');
})->prefix('app\Controller\H5\Api/')->allowCrossDomain([
    'Access-Control-Allow-Origin'        => 'http://域名地址',
    'Access-Control-Allow-Credentials'   => 'true'
]);

取消多个域名的跨域限制

Route::group('/h5', function(){
    Route::get('/xxx/xxx/:id', 'xxxx');
})->prefix('app\Controller\H5\Api/')->allowCrossDomain([
    'Access-Control-Allow-Origin'        => 'http://域名1',
    'Access-Control-Allow-Credentials'   => 'true'
]);
Route::group('/h5', function(){
    Route::get('/xxx/xxx/:id', 'xxxx');
})->prefix('app\Controller\H5\Api/')->allowCrossDomain([
    'Access-Control-Allow-Origin'        => 'http://域名2',
    'Access-Control-Allow-Credentials'   => 'true'
]);

allowCrossDomain()也可以不传数组直接使用,这样就不是针对某个域名了,而是全局解除限制。

官方文档