add "async: false," this line to the $.ajax $.ajax({ url:url, async: false, data:params, success:function(res){ }, error:function(err){ } });