Adding Access-Control-Allow-Origin header response in Laravel

QuestionsCategory: QuestionsAdding Access-Control-Allow-Origin header response in Laravel
jasmeen asked 11 months ago

Adding Access-Control-Allow-Origin header response in Laravel
 
How to solve this?

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Answers
Best Answer
Ajay Malhotra answered 11 months ago

Please check below code and you will get on it:
 
php artisan make:middleware Cors // run command into terminal
 
app/Http/Middleware/Cors.php:
 
public function handle($request, Closure $next)
{
    return $next($request)
        ->header(‘Access-Control-Allow-Origin’, ‘*’)
        ->header(‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, DELETE, OPTIONS’);
}
 
 
app/Http/Kernel.php
 
$routeMiddleware =[

‘cors’ => \App\Http\Middleware\Cors::class, 

];
 
 
routes/api.php
 
Route::get(‘sample-restful-apis’, function()
{
return response()->json([
‘name’ => ‘test’,
‘domain’ => ‘test.com’
]);
})->middleware(“cors”);
 
Thank you.
 

jasmeen replied 11 months ago

Thank you a lot..

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your Answer

18 + 8 =

Positive SSL