ajax请求嵌套页面假死

问题描述

ajax嵌套,beforeSend中对页面的渲染方法没有生效

$.ajax({
            type:'post',
            url:api['selectChina'],
            xhrFields:{withCredentials:true},
            data: result,
            beforeSend:function(){disbtn(btn);},
            success:function(json){
                    $.ajax({
                        type:'post',
                        url:api['save'],
                        xhrFields:{withCredentials:true},
                        data: result,
                        beforeSend:function(){disbtn(btn);},
                        success:function(json){
                            ...
                        },
                        complete:function(){undisbtn(btn,str);},
                        error:function(){showmsg('提交失败,请稍后再试。','0');}
                    })
            },
            complete:function(){undisbtn(btn,str);},
            error:function(){showmsg('提交失败,请稍后再试。','0');}
        })

原因

内外都是异步的请求,导致beforeSend渲染后,又被complete渲染回来了;

改一

将内外都改成同步请求 , 还是不生效

$.ajax({
            type:'post',
            url:api['selectChina'],
            xhrFields:{withCredentials:true},
            data: result,
            async:false,
            beforeSend:function(){disbtn(btn);},
            success:function(json){
                    $.ajax({
                        type:'post',
                        url:api['save'],
                        xhrFields:{withCredentials:true},
                        data: result,
						async:false,
                        beforeSend:function(){disbtn(btn);},
                        success:function(json){
                            ...
                        },
                        complete:function(){undisbtn(btn,str);},
                        error:function(){showmsg('提交失败,请稍后再试。','0');}
                    })
            },
            complete:function(){undisbtn(btn,str);},
            error:function(){showmsg('提交失败,请稍后再试。','0');}
        })

原因

再往上查了一下 有说是因为 这个原因就是ajax的async设置为false时同步提交。浏览器的渲染(UI)线程和js线程是互斥的,在执行js耗时操作时,页面渲染会被阻塞掉。

改二

$.ajax({
            type:'post',
            url:api['selectChina'],
            xhrFields:{withCredentials:true},
            data: result,
            beforeSend:function(){disbtn(btn);},
            success:function(json){
                    $.ajax({
                        type:'post',
                        url:api['save'],
                        xhrFields:{withCredentials:true},
                        data: result,
						async:false,
                        beforeSend:function(){disbtn(btn);},
                        success:function(json){
                            ...
                        },
                        complete:function(){undisbtn(btn,str);},
                        error:function(){showmsg('提交失败,请稍后再试。','0');}
                    })
            },
            complete:function(){undisbtn(btn,str);},
            error:function(){showmsg('提交失败,请稍后再试。','0');}
        })

我将外面的改成异步的,内层保持同步,保证我的渲染效果.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值