Jquery ajax modifies the global variable sample code

  • 2020-03-26 23:46:49
  • OfStack

I had a problem at work today
The code is as follows:
 
var status=1; 
var ob=$("[name='email']"); 
$.ajax({ 
url:"{:U('Home/Index/checkemail')}", 
data:"email="+query.email, 
type:"post", 
dataType:"json", 
success:function(re){ 
status=0; 
} 
}); 
alert(status); 

So every time it pops out, it's going to be 1;
Because I wanted to change status based on the returned value, I looked it up
Solution: async:false,
Rewrite:
 
$.ajax({ 
async:false, 
url:"{:U('Home/Index/checkemail')}", 
data:"email="+query.email, 
type:"post", 
dataType:"json", 
success:function(re){ 
status=0; 
} 
}); 

Related articles: