错误示例:
function checkCP(customerId,productId){
$.ajax({
url:"/cp/checkCP",
type:"post",
dataType:"json",
data:{productId:productId,customerId:customerId},
async: false,
success:function(data) {
alert("data:"+data);
if (data){
$("#cpSpan1").text("该用户已订购该产品!");
return false;
}else{
$("#cpSpan1").text("该用户未订购该产品!");
return true;
}
}
});
}
正确示例:
function checkCP(customerId,productId){
var flag=true;
$.ajax({
url:"/cp/checkCP",
type:"post",
dataType:"json",
data:{productId:productId,customerId:customerId},
async: false,
success:function(data) {
alert("data:"+data);
if (data){
$("#cpSpan1").text("该用户已订购该产品!");
flag=false;
}else{
$("#cpSpan1").text("该用户未订购该产品!");
}
}
});
return flag;
}
原因:
success是一个函数,返回结果的作用域不同。