michaelr524
michaelr524

Reputation: 921

backbone.js model save (PUT request) jquery error

I'm saving a backbone.js model using the model.save() method, which fires a PUT request to the server. The server sees it fine and just sets the status code for response. So far so good but when the request returns (to ie 8) I get a 'null is null or not an object error' somewhere in jquery code. I guess jquery expects something to come back from a PUT a request. When I tried just printing 'ok' to the response stream on the server it didn't throw an exception in the browser but triggered the model.save() error() callback function. Anyone knows what's happening here?

Upvotes: 0

Views: 957

Answers (1)

Brian Genisio
Brian Genisio

Reputation: 48147

Yes. If you are simply returning a header with 200, you aren't doing enough.

You need to return something in the body. Backbone expects it to be the JSON of your object. In the case of a create, this is necessary because you at least need to return the ID of your object so that Backbone knows how to update. In the case of an update, the convention is to (at least) return the things that changed.

In either case, you can just return the entire object... that is what is easiest and I prefer it that way because it ensures consistency with the server.

I suppose you can probably just return {}, and that might be fine for updates, but it would be insufficient for creates.

I'd stick with returning the object in the body.

Upvotes: 3

Related Questions