【发布时间】: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