Backbone.js的混得POST请求数据 [英] Backbone.js getting data on POST request
问题描述
我是新来backbone.js.I我试图用我的服务,POST数据并返回我的数据。我的服务是: http://192.168.1.3:8080/app/search/candidate
这需要作为输入
{
技巧:C,Java的]
}
和它返回的用户列表给我。所以我在骨干创建的集合,并试图
我的code是
VAR的usermodel = Backbone.Model.extend({
});VAR EntityList = Backbone.Collection.extend({
型号:的usermodel,
网址:`的http://192.168.1.3:8080 /应用/搜索/ candidate'` }); VAR查看= Backbone.View.extend({ EL:#mydiv',
模板:_.template($(#范围)HTML()),
初始化:功能(){
VAR自我=这一点;
this.coll =新EntityList();
// this.coll.bind(复位,_.bind(this.render,这一点));
this.coll.fetch({
数据:JSON.stringify({'技能':'C','Java的']}) 输入:POST, 成功:函数(){
//console.log(this.coll.toJSON());
self.render();
}});
}, 渲染:功能(){
//人员将在您的模板看得见
这$ el.html(this.template({人数:this.coll.toJSON()}));
返回此;
} });
VAR视图=新的View();
但它给了我状态code:HTTP / 1.1 415不支持的媒体类型
凡我失踪?
头从CHROM调试请求
请求URL:http://192.168.1.3:8080 /应用/搜索/候选人
请求方法:POST
状态code:415不支持的媒体类型
请求Headersview源
接受:应用/ JSON,文本/ javascript中,* / *; Q = 0.01
接受编码:gzip,紧缩,SDCH
接受语言:EN-US,EN; Q = 0.8
缓存控制:最大年龄= 0
连接:保持活动
内容长度:0
饼干:JSESSIONID = 763119F314E1485DCC8B838F4539BA78
主持人:192.168.1.3:8080
产地:HTTP://本地主机:8080
引用者:HTTP://本地主机:8080 / jobSearch.html
用户代理:Mozilla的/ 5.0(X11; Linux的i686的)为AppleWebKit / 537.36(KHTML,例如Gecko)Ubuntu的铬/ 31.0.1650.63铬/ Safari浏览器31.0.1650.63 / 537.36
响应Headersview源
访问控制允许的凭据:真
访问控制允许来源:HTTP://本地主机:8080
内容长度:0
日期:星期一,2014年2月17日11点46分00秒GMT
服务器:Apache-狼/ 1.1
这code的工作,在控制台中看到的请求头是这样的的jsfiddle :
this.coll.fetch({ beforeSend:功能(XHR){
xhr.setRequestHeader('内容类型,应用/ JSON');
}, 数据:JSON.stringify({'技能':'C','Java的']}) 输入:POST, 成功:函数(){
//console.log(this.coll.toJSON());
self.render();
}
});
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();
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
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屋!