How to enable Access-Control-Allow-Origin for angular 5/nodejs?

前端 未结 4 492
名媛妹妹
名媛妹妹 2021-01-20 16:43

Read many ways for including of \'Access-Control-Allow-Origin\' and none worked for me.

I use @angular/common/http module and external url as data source. by the att

4条回答
  •  盖世英雄少女心
    2021-01-20 17:25

    **Set headers to allow CORS origin in Express **

    => Add code in the server.js file or mail file.

    app.use(function(req, res, next) {
       res.header("Access-Control-Allow-Origin", "*");
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
     });
    

    CORS (Cross-Origin Resource Sharing) is an HTML5 feature that allows one site to access another site’s resources despite being under different domain names.

    The W3C specification for CORS actually does a pretty good job of providing some simple examples of the response headers, such as the key header, Access-Control-Allow-Origin, and other headers that you must use to enable CORS on your web server.

提交回复
热议问题