ajax 同步

百度上说async:false,就是同步了。测试后发现不是

<script type="text/javascript">
    function callAjax() {
        var value = '1';

        jQuery.ajax({
            async:false,
            url:"Handler1.ashx",
            //type:"post",
            success:function(msg){
                value = msg;
            }
        });
   
        return value;
    }
    $(function myfunction() {
        alert(callAjax());
    });
</script>

结果还是返回 1.

关键方法来了:
jQuery.ajax({
async:false,
url:"Handler1.ashx",
//type:"post",
success:function(msg){
value = msg;
}
}).done(function(msg){
value = msg;
});

加个done才是真正的同步。返回后台数据

猜你喜欢

转载自www.cnblogs.com/wolf12/p/9988122.html