繁体   English   中英

多个ajax jquery请求问题

[英]Multiple ajax jquery request issue

该应用程序必须通过$ .ajax请求Web服务,但是时间会是多次,并且取决于用户。 由于它是动态的,似乎$。当不能为它工作。

这是代码:

var increase=0;
    var test_use_upload_file_name= t2_image_path+ increase;
var soap_add_new_story_image=
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + 
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
            'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
                '<soap:Body>'+
                    '<AddNewStoryImage xmlns="http://x.x.x.x/StoryForMac/">'+
                        '<StoryID>'+story_id+'</StoryID>'+
                        '<UserName>'+User_Name+'</UserName>'+
                        '<DragNumber>'+Drag_Number+'</DragNumber>'+
                        '<ImagePath>'+test_use_upload_file_name+'</ImagePath>'+
                    '</AddNewStoryImage>'+
                '</soap:Body>'+
            '</soap:Envelope>';   


//multiple ajax request
var start= 0;
for(;start< **USER_Decide**;start++)
{
    $.ajax({
        type: "POST",
        url: webServiceAddNewStoryImgUrl,
        contentType: "text/xml",
        dataType: "xml",
        data: soap_add_new_story_image,     
        success: process_add_new_img_Success,
        error: process_add_new_img_Error        
    }); 
    increase++;
    test_use_upload_file_name= t2_image_path+ increase; 
}

由于我不知道用户会绘制多少图片,因此每次都必须更新文件名(增加++)。
请提出任何建议〜谢谢!

更新 :对不起,我的表情不好。 这段代码“没用”。我的问题与此类似。请看一下。但是不同之处在于,我不知道我必须调用ajax请求多少次,因为用户会确定它。因此,我可以这里不提供$ .when方法,是否清楚?

选中此选项,这是实现目标的一种可能方法http://jsfiddle.net/oceog/mNegP/

var object = {
    a: {
        id: 'aaa',
        text: 'bbb'
    },
    b: {
        id: 'ccc',
        text: 'cccc'
    }
};
var json = JSON.stringify(object);
console.log("JSON:", json);

//that will run callback when counter is zero
var check_success = function(num, callback) {
    console.log("one done",num,"left");
    if (num <=0) {
        callback();
    }
}

var checkloop = function() {
        var done=0;
      var num=10;// (decide by user);
//this will be called only if all requests done
var run_onlast=function() {
    console.log("last done");
    };
    //at this point we know how many ajax we would run in row
    for (var i = 0; i <= num; i++) {
        console.log("start",i);
        $.post('/echo/json/', {
        //just for sample, random delays
            json: json,delay: Math.round(Math.random()*4)
        },  "json").success(function(data) {
        //run our check function provide it with our last function and our callback
            check_success(num,run_onlast)
        }).error(function(data) {

            console.log({
                error: data.responseText
            });
        }).complete(function() {
        //always reduce, even if error
            num--;
        });
    }
        done=1;
}
    
    checkloop();
​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM