1
votes

Failed to load https://example.com/api/api_details: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example-international.firebaseapp.com' is therefore not allowed access.

That is my error when requesting a POST method. But its perfectly alright when it was in local and i put these line in header on laravel function:

header('Access-Control-Allow-Origin: *');

I also tried these for online: header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');

But no luck. All the post and get request are okay in local but in online only get request works. I am using angular 6 and laravel 5.3.

here is my network tab given bellow: enter image description here

2
500 Internal Server Error ⬅ That would seem to be the problem you need to fix. The reason the response doesn’t have an Access-Control-Allow-Origin error is because it’s a 500 error response, not a success response — and because whatever internal server error occurred, it could have happened before the server ever even gets around to running your application code. - sideshowbarker
@sideshowbarker are you sure that 500 means that is not a cors issue. cause my online and local server side code is same. - Mohammad Tanveruzzaman

2 Answers

2
votes

Use this package inside your Laravel application.

https://github.com/barryvdh/laravel-cors

It's very simple and will solve your problem.

Just don't forget to publish the config file using:

$ php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

0
votes

create a cors middleware and replace your handle method with

public function handle($request, Closure $next)
{
   return $next($request)
       ->header('Access-Control-Allow-Origin', '*')
       ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
       ->header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type');
}