I'm creating a backbone app that's connecting to a RESTful backend. When I call save() on a model, it sends the post data as stringified JSON:
{"firstName":"first","lastName":"last","Email":"[email protected]"}
but my server expects it to be formatted like a querystring:
firstName=first&lastName=last&[email protected]
is there a way to have backbone send it differently?
If the form uses POST, the form data is placed in the request body. For POSTed data, the enctype attribute specifies the format of the request body: Form data is encoded as name/value pairs, similar to a URI query string. This is the default format for POST. Form data is encoded as a multipart MIME message.
For POSTed data, the enctype attribute specifies the format of the request body: Form data is encoded as name/value pairs, similar to a URI query string. This is the default format for POST. Form data is encoded as a multipart MIME message.
If the form uses GET, the form data is encoded in the URI as a query string. If the form uses POST, the form data is placed in the request body. For POSTed data, the enctype attribute specifies the format of the request body: Form data is encoded as name/value pairs, similar to a URI query string.
For POSTed data, the enctype attribute specifies the format of the request body: Form data is encoded as name/value pairs, similar to a URI query string. This is the default format for POST.
Backbone doesn't provide anything like this out of the box.
But is easy to override and customize it to your needs.
Have a look to the source code: http://documentcloud.github.com/backbone/docs/backbone.html
and check out that calling save
, it will trigger a sync
call in the background.
So what you need is to override Backbone.sync
function with your own.
I would modify the part of:
if (!options.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
params.data = JSON.stringify(model.toJSON());
}
with
if (!options.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
params.data = $.param(model); // <-- CHANGED
}
Notice I'm using jQuery param
If you want to use a custom function, check this question: Query-string encoding of a Javascript Object
[Update.]
No need to modify directly. Better override it with your own function 'Backbone.sync'
Check the "TODO" example of the Backbone repository. It has a localStorage.js
file that overrides Backbone.sync function https://github.com/documentcloud/backbone/tree/master/examples
I ran into this problem at work and the Backbone.emulateJSON didn't work for me either. With some help I was able to come up with this workaround. We overrode the Backbone.ajax function and changed the contentType to "application/x-www-form-urlencoded" and used $.param to properly serialize the data.
Backbone.ajax = function() {
if(arguments[0].data && arguments[0].contentType == "application/json"){
arguments[0].data = $.param(JSON.parse(arguments[0].data));
arguments[0].contentType = "application/x-www-form-urlencoded";
}
return Backbone.$.ajax.apply(Backbone.$, arguments);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With