Symfony 4 api Restful No 'Access-Control-Allow-Origin' header is present on the requested resource, only for one resource

Y.Coding picture Y.Coding · Jun 20, 2018 · Viewed 8k times · Source

I am using Symfony 4 as Restful API, I was working perfectly with calling the api from a VueJS application. Today, I needed to add an another resource on the api but when I want to call it to get data from Vuejs I get "No 'Access-Control-Allow-Origin' header is present on the requested resource". Only for this entity, for the others it's working.

enter image description here

Answer

Denis V picture Denis V · Jun 20, 2018

Your problem is related to CORS:

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin. A web application makes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, and port) than its own origin.

One of the possible solutions can be installing NelmioCorsBundle. This will bring CORS handling directly to your application.

Another solution can be adding this header in your web server (How do I add Access-Control-Allow-Origin in NGINX?).