I'm doing a pretty basic Backbonejs model.save:
this.model.save(null, {
success: function(model, response)
{
},
error: function(model, response)
{
}
});
The model contains some string attributes, and an array of objects. When I look at the raw ajax request that Backbone makes (using jQuery ajax, I believe), it wraps the array in double quotes. This causes my endpoint to try to parse the value as a string, rather than an array:
{
id: 108,
name: "My model",
questions: "[{"id": 100, "name": "question 1"}, {"id": 101, "name": "question 2"}]"
}
Has anyone encountered this before? Is there a way to force Backbone/jQuery to not wrap the array in quotes, i.e.:
questions: [{"id": 100, "name": "question 1"}, {"id": 101, "name": "question 2"}]
Update, backbone's sync function calls JSON.stringify on the output of model.toJSON, and JSON.stringify is what is adding those quotes. For example:
console.log(JSON.stringify({name: 'test1', animals: ['horse', 'pig']}));
Outputs:
{"name":"test1","animals":"[\"horse\", \"pig\"]"}