Javascript 多个ajax jquery请求问题
应用程序必须通过$.ajax请求web服务,但是请求的次数是多次的,这取决于用户。因为它是动态的,所以它似乎不能工作 代码如下:Javascript 多个ajax jquery请求问题,javascript,ajax,jquery,soap,Javascript,Ajax,Jquery,Soap,应用程序必须通过$.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 xml
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;
}
var增加=0;
var测试\使用\上传\文件\名称=t2 \图像\路径+增加;
var soap\u添加\u新的\u故事\u图像=
'' +
''+
''+
但不同的是,我不知道我需要调用ajax请求多少次,因为由用户决定。
因此,当提供了时,我不能使用方法$。
清楚吗?…检查这一点,这是实现目标的一种可能方法
var对象={
a:{
id:'aaa',
文本:“bbb”
},
b:{
id:'ccc',
正文:“中交”
}
};
var json=json.stringify(对象);
log(“JSON:,JSON”);
//当计数器为零时,将运行回调
var check_success=函数(num,回调){
log(“一个完成”,num,“左”);
如果(num那么问题到底是什么?你想要一个更好的模型吗?如果你有一组html标记,它们都具有相同的类并存储图像路径,那么你可以使用为每个循环遍历这些标记,并从循环中当前检查的标记获取图像url。请检查更新的通知。实际上,我相信(如果我正确理解您的用例)您可以在
时使用,使用应用
。请参阅。@MasterAM我将尝试使用它。现在我尝试通过递归调用解决它:如果$.ajax调用成功,并且方法进程\u add\u new\u img\u Success将再次调用$.ajax。尝试…尽管我可以通过递归调用方法解决此问题(我在上面的评论。),我仍然会尝试一下。如果它真的可以工作,并且没有其他更好的解决方案出现,我会选择它作为答案。谢谢你。正如我对递归的理解,你将一个接一个地运行所有查询,
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();