Restify 和 vue cors 跨域被屏蔽
Restify and vue cors cross domain blocked
我有一个 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 预检通道未成功)。
我做错了什么?
变化:
server.pre(restify.CORS())
至:
server.use(restify.CORS())
这是因为 restify.CORS() 在 restify 新版本中被弃用了,至少我在这里看到了:
https://github.com/restify/node-restify/issues/1151
"The restify CORS plugin will be deprecated in the upcoming 5.x release in favor of https://github.com/TabDigital/restify-cors-middleware"
我更改了这个插件,然后一切又恢复正常了。
我有一个 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 预检通道未成功)。
我做错了什么?
变化:
server.pre(restify.CORS())
至:
server.use(restify.CORS())
这是因为 restify.CORS() 在 restify 新版本中被弃用了,至少我在这里看到了:
https://github.com/restify/node-restify/issues/1151
"The restify CORS plugin will be deprecated in the upcoming 5.x release in favor of https://github.com/TabDigital/restify-cors-middleware"
我更改了这个插件,然后一切又恢复正常了。