Backbone model.save() not calling either error or success callbacks

backbone.js

backbone.js Problem Overview


I'm trying to update a record in DB so I'm defining model with data and calling .save() method. The PUT request is triggered and the database entry is updated. The problem is neither success or error callbacks are called. What could be the cause?

sessionsModel.save({
    error: function() {
        alert('test');
    },
    success: function () {
        alert('test');
    }
});

Edit: Request returns JSON object

backbone.js Solutions


Solution 1 - backbone.js

Just found similar problem where the issue was solved. You have to put something as first parameter (I put null since my model was already populated with data explicitly) and object with callbacks as second. So something like;

sessionsModel.save(null, {success:function() {} });

Solution 2 - backbone.js

While searching on this, I first landed on this SO thread which did not work for me, but seemed to work for other, later on I bumped into this link, where some one had tried null instead of {} as the first parameter.

this.model.save(null, {
    success: function (model, response) {
        console.log("success");
    },
    error: function (model, response) {
        console.log("error");
    }
});

so, this worked for me. Hope this helps you too.

Solution 3 - backbone.js

Your server must return a JSON object. If the response is not a JSON object, the callbacks will not fire. Check this solution https://stackoverflow.com/a/22176044/1579718

Solution 4 - backbone.js

I was suffering this issue - but was struggling because my server was responding with a valid JSON object (the model) and I was already using null in my save call.

As I (eventually) found, before the success callback is fired, the returned model is passed through the validate method. In my case I had a (obvious when you're looking in the right place) problem which caused the returned model to be deemed invalid and subsequently prevent the success callback.

Whilst I appreciate this doesn't help the OP, I post this in the hope it helps someone else having the same issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmarcin_kossView Question on Stackoverflow
Solution 1 - backbone.jsmarcin_kossView Answer on Stackoverflow
Solution 2 - backbone.jsYasser ShaikhView Answer on Stackoverflow
Solution 3 - backbone.jsIgor G.View Answer on Stackoverflow
Solution 4 - backbone.jsMarkView Answer on Stackoverflow