Angular 2 - No 'Access-Control-Allow-Origin' header is present on the requested resource

后端 未结 4 560
醉话见心
醉话见心 2020-12-11 03:55

When trying to make API Calls from my Angular 2 App to my API, I get the following error:

XMLHttpRequest cannot load http://localhost/myAPI/public/api/v1/aut         


        
4条回答
  •  鱼传尺愫
    2020-12-11 04:31

    //Add this middleware in your express app
    app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('access-Control-Allow-Origin', '*');
    next();
    });
    

提交回复
热议问题