简体   繁体   中英

Laravel API CORS

I created API using Laravel 5.6 and i created CORS middleware and this is the content of middleware , but when access this api from another Ip address it's return the data ..

Route::group([

'middleware' => 'cors',

], function ($router) {


Route::get('/get_users', function(){
     return App\User::all();
});

});

I registered the cors middleware in Kernal.php

您可以使用barryvdh / laravel-cors软件包来代替编写自己的中间件。

Just add this to the beginning of your api route file:

header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Headers : Content-Type,X-Auth-Token,Authorization,Origin');
header('Access-Control-Allow-Methods :GET, POST, PUT, DELETE, OPTIONS');

This work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM