Reputation: 11
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
Upvotes: 1
Views: 2897
Reputation: 344
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.
Upvotes: 1
Reputation: 4942
You may use barryvdh/laravel-cors package instead of writing your own middleware.
Upvotes: 1