How to set up synchronization for ajax function execution order in jquery

  • 2020-03-30 02:11:40
  • OfStack

JavaScript code
 
function existProduct(obj){ 

var productname = escape(obj.val()); 
$.getJSON("../product/searchProductByName.action",datat,function(data){ 
var falg; 
falg = data[0]['existproduct']; 
//Falg returns no or yes,
return falg; 
}); 

} 

function validateform(){ 
//alert(existProduct($("#products_name"))); 
if(validateNotnull($("#products_name"))){ 
if(existProduct($("#products_name"))){ 
$("#productform").submit(); 
}else{ 
$("#spanproduct").html(" The business name already exists "); 
} 
}else{ 
$("#spanproduct").html(" The business name cannot be empty "); 
} 
} 

In the existProduct(obj) function, I call the getJSON function in jquery to return the result of the search in the database, which should be no problem to check with firebug, but when the code executes, the code first executes return falg, and then executes getJSON

Related articles: