使用CORS仍然会产生交叉原点错误

我试图通过使用cors模块angular$http访问节点路由。 我试过一个简单的

 app.use(cors()); 

但仍然得到错误。 我已经尝试从cors文档中添加一个URL白名单

 var corsOptions = { origin: function(origin, callback){ var originIsWhitelisted = whitelist.indexOf(origin) !== -1; callback(null, originIsWhitelisted); } }; app.get('/someroute/:someparam/', cors(corsOptions), function(req, res, next){ mymodule.getData(req.params.someparam, function(err, data){ if (err){ console.log('error with route', err); }else{ res.json({result: data}); } }); }); 

但是我仍然得到错误

XMLHttpRequest无法加载localhost:8888 / someroute / undefined。 协议scheme仅支持跨源请求:http,data,chrome,chrome-extension,https,chrome-extension-resource。

我以为使用Cors模块是为了避免这个问题。 我该如何解决?

这里的问题是您的客户端需要对URL进行请求

 http://localhost:8888/someroute/undefined 

相反,您的客户正在提出请求

 localhost:8888/someroute/undefined 

浏览器使用schemelocalhost将其解释为对主机8888的请求。 但是, localhost不是Chrome支持CORS的scheme; 只有像httphttpsdata东西。

某处,你的客户端代码就像这样

 xhr.send("localhost:8888/...") 

但它需要一个领先的scheme,如http://https://

请注意,如果您尝试使用file或计划请求资源,则会出现类似的错误。