fetch data from different localhost in Angular

后端 未结 2 1178
醉话见心
醉话见心 2021-01-07 07:23

in Angular, ng serve --port 4200 --host 0.0.0.0 creates an instance of http server. I\'ve been told, that in order to fetch data from server side scripts in Ang

2条回答
  •  甜味超标
    2021-01-07 08:04

    If you have Tow loalhost application, first is Angular app and is running in http://localhost:4200 and another is API project and running in http://localhost:3000, In this situation if from your angular app make request to another, you get CORS error, for solving this problem you can use below instructions step by step:

    1. Make a file and name it proxy.config.json inside main folder of Angular project like below image:

    1. Then put codes inside created file like this (please notice to target part):

      {
          "/api/*": {
              "target": "http://localhost:3000",
              "secure": false,
              "logLevel": "debug"
          }
      }
      
    2. Then Change package.josn file and insert code like below image"

    1. And after all you must use npm start to running anglar project

    Note: If you don't do it for angular project, you will occure with CORS problem.

提交回复
热议问题