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

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

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);可以写想要使用的头

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

上一篇:mysql设置权限,添加远程访问用户


下一篇:mongodb常用命令