javascript - How to abort synchronous ajax request in jquery? -
i'm trying integrate ajax request inside validator.addmethod
jquery validation.
let's have input field like
amount : [ text box ]
and validation rules are
$('.form').validate({ .... ... amount:{ amt_val : true } .. });
and add validator method is
$.validator.addmethod("amt_val", function(value, element){ $.ajax({ type: 'post', async:false, ... ... success:function(res){ // return true or false } }); });
note : reason why i'm using async:false here is, validator returns false before ajax request completes.
every time enter amount validate through ajax. want abort previous ajax requests if continuously type amount value.
tried this solution like
var xhr = null; $.validator.addmethod("amt_val", function(value, element){ if(xhr != null) { xhr.abort(); xhr = null; } xhr = $.ajax({ type: 'post', async:false, ... ... success:function(res){ // return true or false } }); });
but works on async requests. ideas ?
Comments
Post a Comment