jquery - Abort ajax load() from ajaxError -
i have lot of places in code fragment being loaded that:
$('<div />').load('/some/thing/', function() { /* put somewhere */ });
i have ajaxerror
handler:
$(document).ajaxerror(function(event, jqxhr, settings, exception) { if (jqxhr.status = 401) { return alert('401!!'); } if (jqxhr.status = 404) { return alert('404!!'); } return alert('boo!'); });
however, if ajaxerro
r triggered, handler associated load still fired. there way cancel "completed" handler (which used fire load handler) ajaxerro
r.
i have played around deferred, problem seems cannot access completedeferred
object. in jquery ajax.js:
jqxhr.complete = completedeferred.add;
the .add()
method, however, useless. if somehow call .empty()
method of underlying deferred object..
any ideas?
edit: not care aborting http request. done, fine. keep callback (that attached via .load()
) firing.
you have either switch .load
more generalized ajax method, or else detect failure within event handler.
to detect failure:
$('<div />').load('/some/thing/', function(response, status, xhr) { if (status === "error") return; // existing code goes here });
to use more general method:
$.get("/some/thing/").done(function(response) { $("<div />").html(response) // etc etc });
Comments
Post a Comment