Backbone model.destroy() invoking error callback function even when it works fine? Backbone model.destroy() invoking error callback function even when it works fine? javascript javascript

Backbone model.destroy() invoking error callback function even when it works fine?


@David Tuite comment:

"Ok I figured it out. It seems that Backbone expects the JSON response to be a JSON serialization of the record that was destroyed. However, Rails controller generators only return head :ok by default. I changed my JSON response to be render json: @listing_save where @listing_save is the record I just destroyed and it registers a success."

FYI - when you're doing a destroy, you don't need to return the full json for the destroyed model. you can return an empty json hash and it will work just fine. the only time you need to return the json for the model is on a save / update.


I had this same problem. In my delete method on the server (java), I didn't return anything. Just status 200/OK (or 204/No content). And so the "parsererror" problem was caused by jquery trying to convert the empty response into JSON, which failed (since "json" is the default data type).

My solution was to use the "text" dataType instead, which can be set in the options:

model.destroy({ dataType: "text", success: function(model, response) {  console.log("success");}});


Your response must have status code 204 as you won't return any content. Since backbone uses a REST interface you should return different http status codes depending on the task.