放入骨干定制HTTP头 [英] Put a custom http header in backbone
问题描述
我创造与Tastypie一个API,我想从主干网接入的API。
要发送凭据我使用的user_id和API_KEY。我这样做是在Android和卷曲和这项工作很好,但我可以设置从骨干网的HTTP标头。
I am creating an API with Tastypie and I want to access to the API from Backbone. To send credentials I use an user_id and a api_key. I do this in android and with curl and this work great, but I can set the http header from backbone.
在袅袅我使用:
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -H "user_id: 32" -H "api_key: 69950" -X DELETE "http://127.0.0.1:8000/api/v1/deletenote/66/?format=json"
和Android中Java的使用:
and in android java I use:
HttpDelete requestDELETE = new HttpDelete();
requestDELETE.setHeader("Content-type", "application/json");
requestDELETE.setHeader("Accept", "application/json");
requestDELETE.setHeader(Constants.HEADER_USER_ID, user_id);
requestDELETE.addHeader(Constants.HEADER_API_KEY, key);
他们都工作得很好,但是当我尝试这骨干下面,我从其他页面发现后的反应,这并不工作。
Both of them work great, but when I try this in Backbone following the responses that I found in other post from the page, this didn't work.
我想这样的:
var removeNote = new DeleteNoteModel({id:this.model.toJSON().id},{ query:this.model.toJSON().id});
removeNote.destroy({
headers: {'user_id':dataWeb.get("id"),'api_key':dataWeb.get("api_key")}
},{
async:false,
error: function(model, response){
console.log("KO_REMOVE_NOTE");
console.log(response);
},
success : function(model, response){
console.log("OK_REMOVE_NOTE");
console.log(response);
}
}
);
我把标题时,我打电话到呼叫摧毁,但不发送anithing到服务器。
I'm putting the header when I call to the destroy call, but this don't send anithing to the server.
我在一个错误的模式干什么?
What I am doing in a wrong mode?
感谢所有。
推荐答案
Tallmaris答案应该修复它为你,虽然我会建议usign的jQuery的 ajaxSetup 方法来设置标题为默认值,所有Ajax请求,因为我相信你需要他们所有的时间呢吧?
Tallmaris answer should fix it for you though I would recommend usign jQuery ajaxSetup method to setup the headers as default values for all ajax requests as I believe you need them all the time anyway right?
在某处你启动应用程序放在
Somewhere where you launch the App put in
$.ajaxSetup({
headers: {
'user_id':dataWeb.get("id"),
'api_key':dataWeb.get("api_key")
}
});
感谢,你会为自己省下不少重复code了:)保持干燥!
Thanks to that you'll save yourself a lot of repeated code :) keep it DRY!
(当然你需要确保dataWeb在范围可在您启动的应用程序:))
(obviously you'd need to ensure that dataWeb is available in the scope of where you launch the app :) )
这篇关于放入骨干定制HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!