Question

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?

Was it helpful?

Solution

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!");
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top