Backbone.js 在 POST 请求中获取数据 [英] Backbone.js getting data on POST request
问题描述
我是backbone.js 的新手.我正在尝试使用我的服务发布数据,它返回我的数据.我的服务是:http://192.168.1.3:8080/app/search/candidate
输入为
I am new to backbone.js.I am trying to POST data with my service and it returns me data. My service is: http://192.168.1.3:8080/app/search/candidate
It takes Input as
{
"skills":["c","java"]
}
它会向我返回用户列表.所以我在主干中创建了集合并尝试我的代码是
And it returns list of users to me. so I created collection in backbone and tried My code is
var UserModel = Backbone.Model.extend({
});
var EntityList = Backbone.Collection.extend({
model: UserModel,
url: `'http://192.168.1.3:8080/app/search/candidate'`
});
var View = Backbone.View.extend({
el : '#mydiv',
template : _.template($("#details").html()),
initialize : function() {
var self = this;
this.coll = new EntityList();
// this.coll.bind("reset", _.bind(this.render, this));
this.coll.fetch({
data:JSON.stringify({'skills':['c','java']}),
type: 'POST' ,
success: function(){
//console.log(this.coll.toJSON());
self.render();
}});
} ,
render : function() {
// the persons will be "visible" in your template
this.$el.html(this.template({ persons: this.coll.toJSON() }));
return this;
}
});
var view = new View();
但它给了我状态代码:HTTP/1.1 415 Unsupported Media Type我在哪里失踪?来自 chrom 调试器的请求标头是
But it gives me Status Code: HTTP/1.1 415 Unsupported Media Type Where I am missing? Header for request from chrom debugger is
Request URL:http://192.168.1.3:8080/app/search/candidate
Request Method:POST
Status Code:415 Unsupported Media Type
Request Headersview source
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
Connection:keep-alive
Content-Length:0
Cookie:JSESSIONID=763119F314E1485DCC8B838F4539BA78
Host:192.168.1.3:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/jobSearch.html
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/31.0.1650.63 Chrome/31.0.1650.63 Safari/537.36
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8080
Content-Length:0
Date:Mon, 17 Feb 2014 11:46:00 GMT
Server:Apache-Coyote/1.1
推荐答案
这段代码有效,在控制台看到的请求头是这样的 jsfiddle :
This code worked, see the request headers in the console is this jsfiddle :
this.coll.fetch({
beforeSend: function (xhr) {
xhr.setRequestHeader('Content-Type', 'application/json');
},
data: JSON.stringify({'skills':['c','java']}),
type: 'POST',
success: function () {
//console.log(this.coll.toJSON());
self.render();
}
});
这篇关于Backbone.js 在 POST 请求中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!