由于$.post() 和 $.get() 默认是“异步请求”,那如何才能把它们改成“同步请求”来使用呢?其实只需要在使用的时候添加两行代码就可以了。
具体如下:更改一下ajax为同步请求,用完后又把ajax改回成异步请求即可。
在$.post()前把ajax设置为同步:$.ajaxSettings.async = false;
在$.post()后把ajax改回为异步:$.ajaxSettings.async = true;
/** * 检测是否还有子组织 */ function checkCatid(user_catid){ var child; $.ajaxSettings.async = false;// 设置ajax为同步请求 $.post('/index/Api/checkCatid', {user_catid:user_catid}, function(result){ child = result.code; },'json'); $.ajaxSettings.async = true;// 恢复ajax为异步请求 return child; }