Angular cors isssssssue

Joined
Feb 15, 2022
Messages
1
Reaction score
0

Attachments

  • proxy.PNG
    proxy.PNG
    78.7 KB · Views: 12
  • console.PNG
    console.PNG
    139.5 KB · Views: 13
  • network.PNG
    network.PNG
    130.7 KB · Views: 13
Joined
Mar 31, 2023
Messages
95
Reaction score
8
It looks like you are trying to make a request from your Angular app to your Express.js server, but you are running into a CORS (Cross-Origin Resource Sharing) error. This error occurs when a web application running under one domain tries to access resources on a server that is on a different domain.

To solve this error, you need to enable CORS on your Express.js server. You can do this by installing the cors package and adding the following code to your server.js file:
PHP:
const cors = require('cors');

app.use(cors());
This will allow your Angular app to make requests to your Express.js server.

Additionally, you can use a proxy to bypass CORS restrictions. In your Angular app's proxy.conf.json file, add the following configuration:
JavaScript:
{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug"
  }
}
This will redirect any requests to /api/* to your Express.js server running on http://localhost:3000.

Make sure to also update your Angular app's service file to use the correct URL path for the API requests:
JavaScript:
private apiUrl = '/api';
With these changes, you should be able to make requests from your Angular app to your Express.js server without encountering the CORS error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top