i'm trying to add an radar from my angular app hosted on http://localhost:4200 the…
- User
i'm trying to add an radar from my angular app hosted on http://localhost:4200 the backend is many microservices connected to a getaway on http://localhost:8888/ so i got the next error : Access to XMLHttpRequest at 'http://localhost:8888/RADAR-SERVER/radars' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.: get method works fine
- Assistant
This issue is caused by the Cross-Origin Resource Sharing (CORS) policy. When you try to make a request from your Angular app to the backend, the browser blocks the request due to the missing 'Access-Control-Allow-Origin' header in the response.
To fix the issue, you need to enable CORS in your backend (getaway). Here's how you can do it:
- If your backend is developed using Express.js: First, install the 'cors' package by executing this command:
bashnpm install corsThen, add the following code to your
app.jsorserver.jsfile:javascriptconst cors = require('cors'); app.use(cors()); - User
my backend developed using spring boot and its already have the next configuration spring: application: name: gateway-service cloud: discovery: enabled: true gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "http://localhost:4200" allowedMethods: - GET - POST - PUT - DELETE - OPTIONS
- Assistant
It seems like you've already configured CORS for your Spring Boot backend using the application.yml file. However, if you still face the CORS issue, the configuration might not be applied correctly.
Try this alternative approach by creating a separate configuration class for CORS:
-
Create a new class called
CorsConfig.javain your project. -
Add the following code to
CorsConfig.java:
java -