应用程序必须通过 $。ajax 请求Web服务,但时间将是多次,并且取决于用户。由于它是动态的,似乎$.when不适合它。
以下是代码:
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;
}
由于我不知道用户将绘制多少图片,我每次都必须更新文件名(增加++)。
请提出任何建议〜谢谢!
的更新:
抱歉我的表情很差。这段代码WON“T WORK。我的问题类似于这个Please take a look.但不同之处在于我不知道有多少次我必须调用ajax请求,因为用户将确定它。
因此,当提供here时,我无法使用 $。方法。
它清楚了吗?...
答案 0 :(得分:0)
选中此项,一种可能的方法来实现您的目标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();