fetch data from different localhost in Angular

后端 未结 2 1187
醉话见心
醉话见心 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:07

    You can proxy your application on http://localhost by creating a file called proxy.conf.json in your angular project with the following content:

    {
      "/api": {
        "target": "http://localhost",
        "secure": false
      }
    }
    

    and then starting ng serve with the additional parameter --proxy-config proxy.conf.json

    ng serve --port 4200 --host 0.0.0.0 --proxy-config proxy.conf.json
    

    You have to change your call then to include the port and /api prefix:

    this.http.get('http://localhost:4200/api/echo.php').subscribe(data => {
    ...
    

    You can find more details here

提交回复
热议问题