Welcome ninja backbone,
This is my first time using Backbone, so please excuse my noob. In my functionality (part of a larger application) I have a Backbone View vA supported by the mA model (as it should be), and the server side is in Spring MVC with annotated Spring management methods with @RequestBody and @ResponseBody. Jackson works fine for me with Spring.
Now in the application
Backbone.Model |_ BaseModel (custom base model for our app) |_ mA (my model)
mA has its own endpoint, and this trunk successfully calls this when creating a PUT request, that is, when I call save () from the send button's event handler from View vA, for example:
this.model.save ({
success : function(){ alert('Request submitted successfully'); }, error : function(){ alert('Something awful happened.'); }
});
Our BaseModel has the following:
define([], function() { window.BaseModel = Backbone.Model.extend({ ...... }); onSyncError : function(model, response) { switch (response.status) { case 403: [...
Spring controller method:
@RequestMapping(value="/resource/xyz/{id}.json", method = RequestMethod.PUT, consumes = {"application/json"} , produces = {"application/json"}) @ResponseBody public Map<String,String> methodX(@RequestBody XyzDTO xyzDTO){ .... map.put("msg", "success"); return map; }
In addition, before making a save call, I change several attributes of the model, since the server-side DTO has a different structure:
this.model.unset("abc",{ silent: true }); this.model.set( { abc: {id : "2",xyz:{ ... //more code } );
The problem is that the save () call generates a PUT request and successfully calls the Spring endpoint handler, but I get a 200 response code (which I expect), but when I spend the call with Firebug it goes to the onSyncError method and gives me a message about an error (due to the "default" case in it).
The Backbone doc document says: "When returning a JSON response, send the model attributes that have been changed by the server and need to be updated on the client." Well, I donβt need to update the model on the client side, its one of the last screens, and I just need to inform the user about the success / error and redirect it to the main page / control panel.
I read a little more, and it seems that the 200 code as an answer is not enough - there may be JSON analysis errors that lead to synchronization failure.
I checked the answer in Firebug and the JSON response looks like {"msg": "Success"}.
So what could be wrong?