【问题标题】:Emberjs Adding headers into ajax requestEmber Js将标头添加到ajax请求
【发布时间】:2014-04-30 19:54:10
【问题描述】:

我正在尝试为我的 emberjs 应用程序设置请求标头。 虽然在初始化程序中执行相同操作,但它确实被注册和注入,但在请求标头中 client_id 以 [object Object] 的形式出现 这是应用程序启动时触发的初始化程序。

Apikey //app/initializers/apikey.js

export default {
  name: "apikey",
  initialize: function(container, application) {
    //application.deferReadiness();
    var data = {"business" : window.location.host};
    $.ajax({
      url: ENV.apiKeyEndpoint,
      type: "POST",
      dataType: "json",
      contentType: "application/json",
      data: JSON.stringify(data)
    }).done(function(results){
      console.log(results.apikey.client_secret);
      container.register("business:key", results.apikey.client_id, { instantiate: false });
      container.register("business:secret", results.apikey.client_secret, { instantiate: false });
      container.injection('controller', 'apiKey', 'business:key');
      container.injection('route', 'apiKey', 'business:key');
      container.injection('serializer', 'apiKey', 'business:key');
      container.injection('data-adapter', 'apiKey', 'business:key');
    });
  }
};

app/adapters/application.js //app/adapters/application.js

export default DS.RESTAdapter.extend({
  // ToDo:: Add headers so that the backend will get notify which client it is.
  headers: {
    'client_id': function() {
       return this.get("apiKey");
    }.property("apiKey"),
  },
  host: ENV.apiEndpoint     
});

我收到的回复:: request headers

//来自 apikey 初始化器

{
    "apikey": {
        "client_id": "0.0.0.0:4300",
        "client_secret": "kjahsdyau89dfuaoisduoaisu",
        "redirect_uri": "",
        "grant_types": null,
        "scope": null,
        "user_id": null
    }
}

//request headers
Accept: application / json,
text / javascript,
*
/*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
client_id:[object Object]
Connection:keep-alive
Host:api.app
Origin:http://0.0.0.0:4300
Referer:http://0.0.0.0:4300/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1964.2 Safari/537.36*/

【问题讨论】:

  • 我不认为 restadapter.headers 与计算属性兼容,您始终可以在初始化适配器并将其作为 var 引用传递之前检索您的属性。这肯定会奏效
  • 我应该在哪里添加??
  • 如果我这样做 var client = function() { return this.get("apiKey"); }.property("apiKey");在 adapters/application.js 文件的顶部,它仍然给我 [object Object]

标签: javascript jquery ajax ember.js ember-data


【解决方案1】:

Ember 数据does not invoke values in the headers hash if they are functions。您的 client_id 标头没有像我想的那样被调用;它只是被直接强制转换为XMLHttpRequest#setRequestHeader 内部某处的字符串。

您可以覆盖 RESTAdapter#ajaxOptions 来自定义行为,例如:

export default DS.RESTAdapter.extend({
  customHeaders: {
    client_id: function(adapter) {
      return adapter.get('apiKey');
    }
  },

  ajaxOptions: function() {
    var adapter = this;
    var hash = this._super.apply(this, arguments);
    var headers = this.customHeaders;

    if (headers) {
      hash.beforeSend = function (xhr) {
        Ember.keys(headers).forEach(function(key) {
          var value = headers[key];

          if (Ember.typeOf(value) === 'function') {
            // Pass the adapter instance when invoking the `customHeader` functions
            value = value.call(null, adapter);
          }

          xhr.setRequestHeader(key, value);
        });
      };
    }

    return hash;
  }
});

更新:customHeader 函数现在使用适配器作为第一个参数而不是上下文来调用。

【讨论】:

  • 现在未定义
  • 我的示例假设您的适配器上有 apiKey 属性。它可以是计算属性,也可以是通过容器注入的东西。请注意,您不应该向 data-adapter 类型注入任何内容,除非您正在创建像 ember 浏览器插件这样的工具。你想在这里注入的类型就是adapter
  • 我按照你说的做了,去掉了数据适配器,只改成了适配器。但我仍然看不到请求中的标头。
  • apiKey 属性在初始化程序本身中设置。它没有在适配器中定义。
  • 通过在初始化程序中添加container.injection('adapter', 'apiKey', 'business:key'); 行,您将apiKey 属性注入所有适配器。
猜你喜欢
  • 1970-01-01
  • 2016-09-28
  • 2012-12-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多