Backbone.js 发布 500 错误
有人知道如何将帖子的 ContentType 指定为 json/application 吗? 我以为我是,并且骨干默认情况下这样做,但从事实来看,它说它正在获取纯文本(请参阅评论) ,我想我需要找出另一种方法来指定它。
我正在使用 Backbone.js,并且尝试 POST 到不再是只读的 TastyPie API,当我尝试创建模型并 .save() 时收到 500 错误。这是我在此处找到的用于同步的代码片段: http://documentcloud.github.com/backbone/docs/backbone.html #section-124
Backbone.sync = function(method, model, options){
var type = methodMap[method];
var params = _.extend({
type: type,
dataType: 'json'
}, options);
if (!params.url){
params.url = getUrl(model) || urlError();
}
if (Backbone.emulateJSON){
params.contentType = 'application/json';
params.data = params.data ? {model: params.data} : {};
}
if (Backbone.emulateHTTP){
if(type === 'PUT' || type === 'DELETE'){
if (Backbone.emulateJSON) params.data._method = type;
params.type = 'POST';
params.beforeSend = function (xhr){
xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
}
if (params.type !== 'GET' && ! Backbone.emulateJSON){
params.prorcessData = false;
}
return $.ajax(params);
};
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function() {
$('form').submit(function() {
var dict = $('form').serializeObject();
var new_task = new Backbone.Model({
date: toString(dict.date),
name: toString(dict.name),
priority: toString(dict.priority)});
console.log("new_task =" + new_task);
new_task.save();
console.log(dict);
return false;
});
});
});
Does anyone know how to specify the ContentType of the Post to json/application? I thought I was, and backbone did it by default, but judging by the fact it's saying it's getting plain text (see comments), I guess I need to figure out another way to specify it.
I am using Backbone.js and I am trying to POST to the TastyPie API that is no longer read only and I am receiving a 500 error when I try to make a model and .save() it. This is a code snippet I am using for my sync that i found here:
http://documentcloud.github.com/backbone/docs/backbone.html#section-124
Backbone.sync = function(method, model, options){
var type = methodMap[method];
var params = _.extend({
type: type,
dataType: 'json'
}, options);
if (!params.url){
params.url = getUrl(model) || urlError();
}
if (Backbone.emulateJSON){
params.contentType = 'application/json';
params.data = params.data ? {model: params.data} : {};
}
if (Backbone.emulateHTTP){
if(type === 'PUT' || type === 'DELETE'){
if (Backbone.emulateJSON) params.data._method = type;
params.type = 'POST';
params.beforeSend = function (xhr){
xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
}
if (params.type !== 'GET' && ! Backbone.emulateJSON){
params.prorcessData = false;
}
return $.ajax(params);
};
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(function() {
$('form').submit(function() {
var dict = $('form').serializeObject();
var new_task = new Backbone.Model({
date: toString(dict.date),
name: toString(dict.name),
priority: toString(dict.priority)});
console.log("new_task =" + new_task);
new_task.save();
console.log(dict);
return false;
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在代码中设置
Backbone.emulateJSON = true;
。如果将此设置为 true,则会将
contentType
设置为“application/json”,这就是您要查找的内容。您只需要设置此变量一次,因此一个好地方就在表单提交代码上方
Try setting
Backbone.emulateJSON = true;
in your code.If this is set to true, then it sets the
contentType
to 'application/json' which is what you're looking for.You only need to set this variable once, so a good place is right above your form submit code