![](/img/trans.png)
[英]Proper way of handling errors when working with nested objects and arrays
[英]Proper way handling nested ajax request
我想知道是否有任何适当的方法来防止ajax请求在同时调用多个404/500错误(同步或异步)时传递随机的404/500错误
例如,带有一些行和列的简单html表,以显示500-800人的列表以及一些数据
方式:
伪代码:
function createTableData(){
var selected;
var personIds;
//get personIds by selection and write them into table
object.executeJSONRequest(...,
{ _ts: new Date().getTime(),
input: selected
},function(data) {
// write result into table
// fill resourceIds with result param
},..);
getEvents(personIds);
getAttribtues(personIds);
getRoles(personIds);
calculateStuff(personIds);
}
function getEvents(personIds){
object.executeGetRequest('...',
{ _ts: new Date().getTime(),
ppl: personIds
},function(data) {
//add result to table
}
}
function getAttributes(personIds){...} //and so on..
如果我连续调用所有这些方法,则有时在加载属性时有时会出现随机错误,有时在加载角色时有时会正常工作。
如果我称它们嵌套在每个ajax成功块中,例如
function createTableData(){
object.executeJSONRequest(...
{ _ts: new Date().getTime(),
input: selected
},function(data) {
getEvents(..);
});
}
function getEvents(..){
object.executeGETRequest(...
{ _ts: new Date().getTime(),
input: selected
},function(data) {
getAttributes(..);
});
}
function getAttributes(..){ ...,function(data){ getRole(..)}); and so on
一切正常。 如果我尝试类似的解决方案
document.ajaxStart/Stop(function(){ //if table columns i expected are filled do next}
我必须检查所有必要的表列是否已填充数据。
有没有我想到的真正合适的解决方案? (jQuery版本1.2 / IE V7-8 ^^)
“如果可能的话,更新jQuery和IE版本”
首先,为什么要在一个进程中调用多个服务器调用。
最好将所有流程组合到一个请求和响应周期中,用Java代码进行所需的数据处理。不要反复处理请求和响应。
您可以使用任何方法返回返回ajax调用状态的jquery ajax。
$.post("url",function(result,status){{
if(status == success){
//continue....
}else{
alert("Sorry something went wrong");
}
}});
这样,检查所有请求是否成功完成。
我强烈建议您考虑将所有ajax调用合并到一个请求中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.