The 'Access-Control-Allow-Origin' header has a value 'http://localhost:4200' that is not equal to the supplied origin

前端 未结 7 2520
清酒与你
清酒与你 2020-12-20 12:33

(continuation of error message in title) \" Origin \'http://127.0.0.1:4200\' is therefore not allowed access.\"

I am unable to run the same Angular 5 site on two dif

7条回答
  •  青春惊慌失措
    2020-12-20 12:57

    Let's assume your api runs on 8080 and your angular code on 4200.

    In your angular app create a file called proxy.conf.json

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

    Start angular app using this command

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

    With this configuration when you call localhost:4200/api you it will call 8080 and it won't have any CORS error

提交回复
热议问题