I was woking on a Laravel API application for Angular. When I was working locally everything was working fine. But when i deploy the Laravel and Angular applications to the live server, I was getting CORS errors in Angular in browser console.
This issue can be solved by adding appropriate headers in Laravel application. You can set this by adding Middleware in these routes. Here is the middleware file at /app/Http/Middleware/CORS.php
<?php
namespace App\Http\Middleware;
use Closure;
class CORS
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
->header('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token');
}
}
Add the middleware to $routeMiddleware variable in app/Http/Kernel.php
file.
protected $routeMiddleware = [
....
'CORS' => \App\Http\Middleware\CORS::class,
....
];
And add the middleware to route.
Route::get('/users/get-data','[email protected]')->middleware('CORS');
This should work and solve the issue.
I hope it will help you to solve the issue.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
jQuery get Value of Selected Radio Button
jQuery radio button is used when you wan...Bootstrap Typehead Demo with PHP and MySql
In this post, i am going to show you how...Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
Today, we are install laravel 5.5 and in...How to remove empty values from an array in PHP
Use the PHP array_filter() fun...Laravel Livewire Multiple File Upload Example
Larave Livewire is a full-stack web fram...