我有一个运行API的restify服务器,我定义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飞行前通道没有成功)。
我做错了什么?
发布于 2017-08-14 21:10:38
之所以发生这种情况,是因为restify.CORS()在重新定义新版本时是不推荐的,至少我在这里看到了它:
https://github.com/restify/node-restify/issues/1151
“restify插件将在即将发布的5.x版本中被弃用,以支持https://github.com/TabDigital/restify-cors-middleware”
我换了这个插件,一切又恢复了。
发布于 2017-08-06 07:55:39
更改:
server.pre(restify.CORS())
至:
server.use(restify.CORS())
https://stackoverflow.com/questions/45460629
复制相似问题