I am facing the CORS issue with api's from laravel to reactjs.
I am running laravel at 5555 port and reactjs is running on 3000.
I have a middleware to allow the origin of 'http://localhost:5555'. But it still gives me error.
My middleware is below.
`<?PHP
namespace AppHttpMiddleware;
use Closure;
class HandleCors
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$allowedOrigins = ['http://localhost:5555'];
$origin = $request->server('HTTP_ORIGIN');
if (in_array($origin, $allowedOrigins)) {
return $next($request)
->header('Access-Control-Allow-Credentials', 'true')
->header('Access-Control-Allow-Origin', $origin)
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers', 'Content-Type, Cache-Control'); //in case if you
want all (*)
}
return $next($request);
}
}`
This is the custom code I have not installed any package for CORS. How do solve this issue?