【问题标题】:Using Ember-data RESTAdapter with different URL使用具有不同 URL 的 Ember-data RESTAdapter
【发布时间】:2013-06-17 09:18:09
【问题描述】:

当我从 ember-rails 移动到外部 ember 应用程序时,我正在使用具有不同 URL 的 ember 数据,因此 API url 是 http://localhost:5000 而 ember 应用程序是 http://localhost:9000

现在的问题是我需要包含x-appidx-app-secret,但是每当为其中任何一个添加xhr.setRequestHeader() 时,GET 请求就会变为OPTIONS 请求。

当我在同一个域上使用 ember-rails 时,这段代码运行良好,这是问题还是缺少其他东西?

ajax: function(url, type, hash) {
    if (this.headers !== undefined) {
      var headers = this.headers;
      if (hash) {
        hash.beforeSend = function (xhr) {
          // Works fine
          xhr.setRequestHeader('Accept', 'application/vnd.vendor+json;version=1');

          // Changes Request from GET to OPTIONS
          xhr.setRequestHeader('x-vendor-appid', '12412412');
          xhr.setRequestHeader('x-vendor-secret', 'aslkdfjaskldfjasd');
        };
      }
    }
    return this._super(url, type, hash);
}

【问题讨论】:

    标签: jquery ember.js ember-data


    【解决方案1】:

    浏览器正在发送一个 OPTIONS 请求,因为请求的域与您页面的域不匹配。这会触发Cross-Origin Resource Sharing (CORS) 限制。

    您需要响应来自服务器的 CORS 请求,通过设置一些 HTTP 标头字段来告诉客户端允许向域发出 CORS 请求。然后浏览器将发出原始请求。我已经用 ember.js/rails 实现了这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      相关资源
      最近更新 更多