Heroku 上的 Meteor Mobile 服务器 - 不存在 Access-Control-Allow-Origin header

Meteor Mobile Server on Heroku - No Access-Control-Allow-Origin header is present

首先让我说我在网上找到了几个建议的解决方案,但其中 none 似乎对我有用。

问题:

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

我一直收到错误

"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)

这是我到目前为止尝试过的方法:

我在 meteor 启动时设置了 ROOT URL:

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

我尝试像这样设置访问控制,server-side 在 meteor 启动时:

  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();
  });

我尝试使用 browser-policy 包,像这样,server-side 在 meteor 启动时:

  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" 文件中的名称与 "mobile-config.js"

下的应用程序名称相同

我还缺少什么?

编辑:

我也试过将 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));

也尝试启用 pre-flight,像这样:

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

将“*”添加到白名单应该可以完成这项工作。最终解决方案在 config.xml 内,这应该会有所帮助:

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

就是这样。一直以来都是这个问题...