Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS OPTIONS preflight 404 if not routed #1242

Closed
riccardo2k13 opened this issue Nov 22, 2021 · 6 comments
Closed

CORS OPTIONS preflight 404 if not routed #1242

riccardo2k13 opened this issue Nov 22, 2021 · 6 comments
Labels

Comments

@riccardo2k13
Copy link

I'm building an API rest app. I was having issues with CORS preflight fail.
I am not using the $f3->map(). I am mapping every routes in a custom routes.ini

Reading the docs I thought I could not care about OPTIONS, but if I do not route the OPTIONS, it always triggers a 404.

CORS preflight fails

I must add a [OPTIONS /api/... = Ctr->preflight] to handle it, where the preflight method just contains an exit function. Then f3 does the rest of the job

immagine

@ikkez
Copy link
Collaborator

ikkez commented Nov 22, 2021

In order to make it work, you just have to have a route at /api/v1/login .. i.e. when you have defined a GET or POST route for it, the OPTIONS preflight is collecting the available http methods that are available, returning a Access-Control-Allow-Methods: OPTION,GET,POST header. This should work out of the box. 404 means that the router probably has not found a route for the resource the request is trying to access.

@riccardo2k13
Copy link
Author

Thanks for the reply. I had it. What you say is right, but if I specify the attribute [ajax]

Ex: POST /api/v1/login [ajax] = Api_Auth_Controller->login // this makes the OPTIONS fail

While, as you said:
POST /api/v1/login = Api_Auth_Controller->login // this works

@ikkez
Copy link
Collaborator

ikkez commented Nov 22, 2021

Well then it's probably a bug with the ajax flag, which should be ignored in such a case

@Joseffb
Copy link

Joseffb commented Nov 22, 2021

I Second to just use the POST /api/v1/login = Api_Auth_Controller->login method.

I also do this in my middle-ware router (your pre-flight?) to handle any custom headers

$this->fw->copy('HEADERS.Origin','CORS.Origin');
$this->fw->set('CORS.Origin','*');
$this->fw->set('CORS.headers',['token','content-type', 'x-property-id']);

@riccardo2k13
Copy link
Author

In fact I will remove the [ajax] specification. Thanks to all.

@riccardo2k13 riccardo2k13 reopened this Nov 22, 2021
@KOTRET KOTRET added the bug label Nov 25, 2021
ikkez added a commit to f3-factory/fatfree-core that referenced this issue Nov 4, 2022
@ikkez
Copy link
Collaborator

ikkez commented Nov 4, 2022

fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants