52

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

marcin_koss
  • 5,763
  • 10
  • 46
  • 65
  • May be accept few answers so that question/accepted ratio stays 1/2 – Deeptechtons Jul 04 '12 at 04:57
  • You can see [the post][1] here answer clearer, it maybe help you [1]: http://stackoverflow.com/questions/15021768/how-to-get-the-error-messages-in-backbone-js – Ulug'bek Mar 01 '13 at 06:32
  • I am getting this problem only when i run my web application in a newly formatted desktop (Windows 8) whereas in older laptops it works without problem. I am able to see my success callback getting executed in older laptop (with Windows 7 for all browsers). Where as applications keeps waiting in a new laptop with Windows 8 for all browsers. – Tarun Feb 25 '15 at 13:25
  • after exploring various options and analysis, I found that Adding dataType: 'text' and then converting the response to JSON.parse(response) solves my problem which I was facing in Windows 8.1. – Tarun Feb 25 '15 at 14:46

4 Answers4

74

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() {} });
marcin_koss
  • 5,763
  • 10
  • 46
  • 65
  • +1 just got stung with this - do you know if this issue has been fixed? – James Apr 15 '14 at 12:18
  • I've also encountered this problem, and I'm using that code then finally realize that my backend is not returning any response that is why I can't trigger a success neither error callback. Just added for someone forget to check the backend response. :) – lukaserat Jun 01 '14 at 05:24
23

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.

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • If you checkout the backbone js documentation, you'll see that save expects either a null value for the key, val pair or an object: http://documentcloud.github.io/backbone/docs/backbone.html#section-56 Bumping Yasser's response, Hopefully, the documentation gives it a little clarity. – chaseadamsio Jun 04 '13 at 05:10
  • Thanks - saved me a headache! – Fijjit Feb 17 '14 at 11:06
18

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

Community
  • 1
  • 1
Igor G.
  • 6,955
  • 6
  • 26
  • 26
  • 4
    Wow, this was the answer. I was returning just a `200 Success` with No body. Then added an empty object to the response and it worked. – Chetan Shenoy Nov 21 '14 at 20:53
  • I am encountering problems with Windows 8.1 whereas in older OS versions it works without problem. I was able to see Backbone.model.save()'s success callback getting executed in older OS (Windows 7 for all browsers). Where as applications keeps waiting in Windows 8.1 when run with any browsers even when the response type is JSON object. After tring out various options of saving Backbone.Model and analysis, I found that adding dataType: 'text' and then converting the response to JSON.parse(response) solves works in Windows 8.1. – Tarun Mar 16 '15 at 09:35
  • 2
    Thanks for the clue that service has to return JSON. – Sudheer Palyam Jul 30 '15 at 08:29
3

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.

Mark
  • 3,005
  • 1
  • 21
  • 30
  • One more note on this. I'm using Backbone 1.1.2 and it requires the JSON object returned by the server to _exactly_ match the JSON object sent to the server, otherwise no success/error callback is called. In my case, there was just a small mistake which caused this issue: I was sending the object's ID as a string, but the server was returning it as an int. Both the request payload and the response body were valid JSON objects, but they were not equal, which is why no callback was called. I don't know whether more recent versions of Backbone have the same confusing behaviour. – TanguyP Oct 18 '19 at 08:53