Nginx Web浏览器的CORS问题

I'm working on an AngularJS Application which uses an external API and I got a weird issue (CORS). I made a lot of research that does not fix the issue.

When I hit my API the preflight request is good (status 204 No-Content), but when the server responds with a another status code than 2xx (200, 204 etc...) I got the CORS issue.

If my server send back 2xx as status code is alright we have no CORS issue but if another status code is sent here is the problem.

API : Sylex(PHP Framework) running under Nginx

Front application : AngularJS - using webpack

XHR VIEW :

XHR view

Console CORS Error :

Console CORS Error

After a day of search I figured out where the CORS was coming from. The Nginx configuration was ok. It was the API.

During the preflight request (OPTIONS) the server let passed the request because NGINX allowed the origin and header (access-control-allow-origin, access-control-allow-headers),

but when the API sends back the response the headers were not set up.

I fixed it by adding the access-control-allow-origin and access-control-allow-headers to the response.

IMPORTANT: I figured out data weren't recoverable in the response with the 200 status code on the webbrowser. To fix it remove the headers added in the others responses (5xx, 4xx, etc..) (I don't know if its just in my case)

I haven't developed the API that's why it takes me a day to figure it out. Hope I will help someone with this use case :)