Search code examples
phplaravelxmlhttprequestcors

Laravel Fix No 'Access-Control-Allow-Origin' header is present on the requested resource


How to solve

No 'Access-Control-Allow-Origin' header is present on the requested resource.

On Laravel 7

what can I do to the response?

I have tried this, but it doens't include the header.

return response()->header('Access-Control-Allow-Origin','*')->json([
            'error'=>false,
        ]);

Edit 1

I already have FruitCakes Cors added (https://github.com/fruitcake/laravel-cors)

My xhr request is coming from my mobile website which is hosted on a subdomain.

mobile app hosted at x.domain.com

laravel app hosted at domain.com


Solution

  • You can use this package - https://github.com/fruitcake/laravel-cors, it still works on Laravel 7 and the documentation is easy to understand.

    Besides, why don't you update to the current version? Here is the tutorial - https://laravel.com/docs/master/upgrade