Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js Post 500 Error
    text
    copied!<p><strong>Does anyone know how to specify the ContentType of the Post to json/application?</strong> 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.</p> <p>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: <a href="http://documentcloud.github.com/backbone/docs/backbone.html#section-124" rel="nofollow">http://documentcloud.github.com/backbone/docs/backbone.html#section-124</a></p> <pre><code> 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' &amp;&amp; ! 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; }); }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload