1

I am currently building a Ionic Application, using laravel framework for the website. The problem is the following : I got

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

when I try to get json file from my API. I saw that it is an header problem and I need to authorize Access-Control-Allow-Origin. And I did it Access-Control-Allow-Origin: *

When i refresh my page, I see with F12 menu on Chrome :

Response headers
Access-Control-Allow-Origin: *

So, that means that the access is authorized. But when i try to access the link

mywebsite.com/api

from the application, I still have the error No 'Access-Control-Allow-Origin' header is present on the requested resource.

If some experts knows how to fix this issue.

5
  • 1
    Check this package: github.com/barryvdh/laravel-cors Commented Aug 13, 2018 at 13:54
  • @HCK I already tried this and still do the same problem Commented Aug 13, 2018 at 14:10
  • Did you configured properly or just installed it? Commented Aug 13, 2018 at 14:39
  • 1
    I configured it properly, but I found the problem. It was a problem with my .htaccess . Thank you anyway ! Commented Aug 13, 2018 at 14:59
  • Glad you found a solution. PS: Try share it in the answer section, it will be helpful to other people too ;) Have a nice day. Commented Aug 13, 2018 at 15:05

2 Answers 2

1

I found this answer from @asamarcos (https://github.com/barryvdh/laravel-cors/issues/243) :

You have to register HandleCors on Kernel.php as requested in laravel-cors manual. This will add cors in all routes under the 'api' middleware.

    'api' => [
        'throttle:60,1',
        'bindings',
        \Barryvdh\Cors\HandleCors::class,
    ],

Problem is Passport has it's own routes, outside of the API middleware. So you have to set your passport routes on AuthServiceProvider explicitly giving HandleCors as a middleware:

public function boot()
{
    $this->registerPolicies();

    Passport::routes(null, ['middleware' => [ \Barryvdh\Cors\HandleCors::class ]]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I post my solution if someone is in the same case as me. The problem came from the .htaccess, I had login and password to access to the domain, and it blocked the ionic application, just desactivate them (No others solutions works).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.