Heroku上的Meteor Mobile服务器 - 没有Access-Control-Allow-Origin标头

时间:2017-07-22 10:31:37

标签: android meteor heroku cors meteor-mobile

首先我要说的是,我已经在网上找到了几个提议的解决方案,但它们似乎都不适合我。

问题:

我有一个流星应用程序我试图在Android上运行。为此,我已在Heroku上部署了应用程序,并使用run android-device参数调用--mobile-server https://myapp.heroku.com命令。

我永久收到错误

"XMLHttpRequest cannot load https://myapp.heroku.com/sockjs/... . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:12848' is therefore not allowed access. The response had HTTP status code 404.", source: http://localhost:12848/ (0)

以下是我迄今为止所尝试的内容:

我在流星启动时设置ROOT URL:

  process.env.ROOT_URL = "https://myapp.heroku.com";  

我尝试在流星启动时设置这样的访问控制,服务器端:

  WebApp.connectHandlers.use(function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
    res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
    res.header('Access-Control-Allow-Origin', 'https://myapp.heroku.com');
    res.header('Access-Control-Allow-Origin', 'http://localhost:12848');
    res.header('Access-Control-Allow-Origin', 'http://meteor.local');
    res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Custom-Header");
    return next();
  });

我试图在meteor startup上使用浏览器策略包,例如服务器端:

  BrowserPolicy.content.allowSameOriginForAll();
  BrowserPolicy.content.allowOriginForAll('*');
  BrowserPolicy.content.allowOriginForAll('http://meteor.local');
  BrowserPolicy.content.allowOriginForAll('https://myapp.heroku.com');
  BrowserPolicy.content.allowOriginForAll('https://*.myapp.heroku.com');
  BrowserPolicy.content.allowEval();

我尝试将访问规则添加到" mobile-config.js":

App.accessRule("*");

我确定了" package.json"中的名字。 root下的文件与" mobile-config.js"

下的App名称相同

我还缺少什么?

修改

我还尝试将express和cors包添加到白名单本地主机:

  var whitelist = [
  'http://localhost:3000',
  'http://localhost:12848',
  'https://myapp.heroku.com'
  ];
  var corsOptions = {
      origin: function(origin, callback){
          var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
          callback(null, originIsWhitelisted);
      },
      credentials: true
  };
  app.use(cors(corsOptions));

还尝试启用飞行前,如下所示:

app.options('*', cors())

2 个答案:

答案 0 :(得分:1)

这可能是我遇到的最愚蠢的问题。尝试使用--mobile-server https://myapp.heroku.com参数运行应用程序是错误的。相反,它应该是https://myapp.herokuapp.com

就是这样。这一直是个问题......

答案 1 :(得分:0)

添加' *'白名单应该做的工作。最终的解决方案在config.xml中,这应该是有用的:https://stackoverflow.com/a/36124935/8056323