Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Send Post data encoded as query string

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?

like image 925
Adam Langsner Avatar asked Jul 10 '12 15:07

Adam Langsner


People also ask

How is form data encoded in a POST request?

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.

What is enctype in post form?

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.

How is form data encoded in a form?

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.

What is the format of the request body for post?

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.


2 Answers

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

like image 83
corbacho Avatar answered Nov 15 '22 08:11

corbacho


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);
}
like image 36
Dan Avatar answered Nov 15 '22 10:11

Dan