我正在使用Tastypie创建API,我想从Backbone访问API.要发送凭据,我使用user_id和api_key.我在android和curl中这样做,这工作很棒,但我可以设置骨干的http标头.
在卷曲我使用:
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"
Run Code Online (Sandbox Code Playgroud)
在android java中我使用:
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);
Run Code Online (Sandbox Code Playgroud)
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.
I am trying this:
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){ …Run Code Online (Sandbox Code Playgroud) 我用Tastypie-Django编写了一个API,我想用Backbone做一个网页来更简单地访问模型.我在Backbone中创建了一个这样的Model和一个这样的集合:
var Abstract = Backbone.Model.extend({
defaults : {
}
});
var AbstractCollection = Backbone.Collection.extend({
model: Abstract,
url : "http://192.168.0.195/api/v1/abstract/?format=json"
});
Run Code Online (Sandbox Code Playgroud)
它在View中的fetch方法就像这样:
var abs = new PocketsAbstractCollection();
abs.fetch({
success: function (collection, response) {
console.log(abs.length);
console.log(abs.models);
}
});
Run Code Online (Sandbox Code Playgroud)
问题是我从这个表单收到一个JSON:
{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 12}, "objects": [{ ... }]}
Run Code Online (Sandbox Code Playgroud)
当我在属性中看到集合的模型时,我有2个元素,一个元素和一个带有元素的对象数组.如何访问"对象数组"元素?
如果我写abs.attributes这给我一个错误.
attributes: Object
meta: Object
objects: Array[12]
0: Object
1: Object
2: Object
3: Object
4: Object
.
.
.
length: 12
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?
谢谢!!