【问题标题】:Restify and vue cors cross domain blockedRestify 和 vue cors 跨域被阻止
【发布时间】:2018-01-09 16:00:16
【问题描述】:

我有一个 restify 服务器运行我的 API,我定义 cors 中间件如下:

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser({
  multiples: true,
  mapParams: false
}));
server.pre(restify.CORS())
server.use(restify.fullResponse())
server.use(
  function crossOrigin(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
  return next();
});

但我总是收到此错误消息:

跨域请求被阻止:同源策略不允许读取位于https://myroute...的远程资源(原因:CORS 预检通道未成功)。

我做错了什么?

【问题讨论】:

    标签: cors middleware restify


    【解决方案1】:

    发生这种情况是因为 restify.CORS() 在 restify 新版本中已被弃用,至少我在这里看到:

    https://github.com/restify/node-restify/issues/1151

    “restify CORS 插件将在即将发布的 5.x 版本中弃用,取而代之的是 https://github.com/TabDigital/restify-cors-middleware

    我换了这个插件,一切都恢复正常了。

    【讨论】:

    • 我使用过 restify-cors-middleware 但仍然面临这样的问题 Access to XMLHttpRequest at 'localhost:3000/login' from origin 'localhost' has been blocked by CORS policy: Request header field access- Access-Control-Allow-Headers 在预检响应中不允许使用 control-allow-credentials。请帮忙解决这个问题。它吞噬了我一整天
    【解决方案2】:

    变化:

    server.pre(restify.CORS())

    到:

    server.use(restify.CORS())

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 2021-10-03
      • 2016-01-21
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2019-08-05
      • 2021-10-13
      • 2021-06-26
      相关资源
      最近更新 更多