1

I'm pretty new to backbone and how it works and inherited a bunch of code but I can't solve this at all:

I have a user model:

var User = Backbone.Model.extend({
    idAttribute: 'username',
    defaults: {
        username: "",
        email: "",
        roles : [],
        password: ""
    }

});

var Users = Backbone.Collection.extend({
    model: User,
    initialize: function(args, options) {
        if (options && options.dialog) {
            this.dialog = options.dialog;
        }
     },

    parse: function(response) {
        if (this.dialog) {
            this.dialog.populate(response);
        }
       return response;
    },

    url: function() {
        var segment = AdminUrl + "/users";
        return segment;
    }
});

Then elsewhere in my view I'm doing:

user.save({username: $newtarget.val()},null);

or user.save();

The PUT is fired to the correct url but every time its triggered it sends the data

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

but my Jersey endpoint accepts application/json

Everywhere I read people are struggling to put urlencoded data but my problem is the otherway around!

Parameters are being send as url params:

 username=admin&email=&password=admin&roles%5B%5D=ROLE_USER&roles%5B%5D=ROLE_ADMIN&id=1

===EDIT===

If I force the content type and data:

    user.save({}, {data: JSON.stringify(user.attributes),contentType: "application/json"});

The put works fine, which is bizarre.

bugg
  • 61
  • 6

1 Answers1

1

Backbone.emulateJSON = false;

is true for some reason

From the docs

Turn on emulateJSON to support legacy servers that can’t deal with direct application/json requests … will encode the body as application/x-www-form-urlencoded instead and will send the model in a form param named model.

http://backbonejs.org/docs/backbone.html

exussum
  • 18,275
  • 8
  • 32
  • 65
  • Thanks for replying, its not, also the docs say that it would post parameters as json "will send the model in a form param named model." but its passing mine as url parameters: username=admin&email=&password=admin&roles%5B%5D=ROLE_USER&roles%5B%5D=ROLE_ADMIN&id=1 – bugg Jun 10 '14 at 15:48
  • What does the put contain ? – exussum Jun 10 '14 at 15:51
  • Can you make a JS fiddle ? – exussum Jun 10 '14 at 15:52