4

I have a jqXHR object which I get from a backbone collection:

var xhr = this.collection.fetch({
  error: function() { alert("oh noes!"); }
});

Sometimes I then need to call xhr.abort(). But this is also triggering the error callback.

How can I call xhr.abort() without triggering an error?

m0sa
  • 10,712
  • 4
  • 44
  • 91
user1031947
  • 6,294
  • 16
  • 55
  • 88

1 Answers1

6

The error function will always be called. But you can check if it was an abort in the error function and ignore it:

    var xhr = this.collection.fetch({
        error: function(model, jqXHR, options) {
               if (jqXHR.textStatus != "abort")
                  alert("oh no!");
        }
    });
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • I I know this was a long time ago, but I found the attribute to be statusText, not textStatus. – noahpc Mar 23 '17 at 14:40