node——Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response

我在请求头中添加了Authorization,结果出错,有跨越问题。

解决方法:

const allowHeaders = "Origin, Expires, Content-Type, X-E4M-With, Authorization";
app.all("*", function (req, res, next) {
  //设置允许跨域的域名,*代表允许人员域名跨域
  res.header("Access-Control-Allow-Origin", "*");
  //允许的header类型
  res.header("Access-Control-Allow-Headers", allowHeaders);
  //允许的header类型
  res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
  if (req.method.toLowerCase() == 'options')
    res.send(200);  //让options尝试请求快速结束
  else
    next();
})

在这里res.header("Access-Control-Allow-Headers", allowHeaders);可以写想要使用的头

posted @ 2020-08-18 13:52  ellenxx  阅读(1638)  评论(0编辑  收藏  举报