jquery 循环调用ajax发送请求

来源:互联网 发布:金融 大数据 微线索 编辑:程序博客网 时间:2024/05/19 12:15

jquery 循环调用ajax发送请求,如果用

for(){

$.ajax();

}

这种方式会出现问题:for完了,但是ajax并没有请求完成,也容易出现对象重用,等错误

注,就算使用ajax同步,async:false,也会出现获取成功返回值出错!


解决方案:递归调用

var times= xmlStrArr.length;
//递归发送请求
CloudiaTransfer_func(times,xmlStrArr);//xmlStrArr是我遍历的参数数组

function CloudiaTransfer_func(times,xmlStrArr){
    if(times <= 0){
        return;
    }
    (function($){
        temp = times-1;
        $.ajax({
            //cache: true,
            type: "get",
            url:"http://127.0.0.1:6789/?Request=CloudiaTransfer",
            data:xmlStrArr[temp],
            cache:false,
            async:false,
            dataType:"jsonp",
            jsonp:"success_jsonpCallback",//服务端用于接收callback调用的function名的参数
            jsonpCallback:"success_jsonpCallback",//callback的function名称
            error: function(request) {;
                //console.log(request);
                alert('error:'+temp)
            },
            success: function(data) {
                //$("#"+ContentID).html('上传中');
                $(".locState span").html('上传中');
                var Result = data[0].Result;
                alert('ok:'+temp)
                times --;
                CloudiaTransfer_func(times,xmlStrArr); //递归调用
            }
        });
    })(jQuery);

}

0 0
原创粉丝点击