jquery - How to cancel an ajax request in Django when new page is requested -
jquery - How to cancel an ajax request in Django when new page is requested -
in web page (built on django platform), have jquery requests server info via ajax request. how prevent error function executing when click away page before ajax request has responded.
here jquery call:
$(document).ready(function() { $.ajax( { url:'/server/url/', type: 'post', data: {some..data}, success:function(response) { stuff response }, error: function() {alert('error')} });
how prevent alert happening when click away page (to new page)?
define global variable:
var ajaxcancel = false;
no need create true
if user closes window:
$(window).bind("beforeunload", function() { ajaxcancel = true; });
and alter ajax error line check value of variable first:
... error: function() {if (!ajaxcancel) alert('error'); } ...
jquery ajax django
Comments
Post a Comment