我有一个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();
})
您也可以尝试此
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();
});