从角度服务调用节点api时出现Cors问题

问题描述 投票:0回答:1

我有一个Angular7前端,我正在尝试从Node服务器发送一个get请求。但是,节点服务器本身无法接收api请求。

我在节点的app.js代码上都尝试过:1>

const cors = require('cors');
app.use(cors());

2>

app.use((req,res,next)=>{
res.header('Access-Control-Allow-Origin', 'http://localhost:4200');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, 
Content-Type, Accept');
     next();
 })
node.js cors angular7
1个回答
0
投票

您也可以尝试此

let cors = require("cors");

app.use(cors(), function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:4200"); // update to match the domain you will make the request from
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});
© www.soinside.com 2019 - 2024. All rights reserved.