是的,这是可能的。查询是否必须连续执行?如果没有,您可以并行运行所有查询。
每个 AJAX 调用都可以选择在成功完成后调用函数。当每个 AJAX 调用完成时,在该代码的底部指定返回数据格式和表示代码函数以及随后的 AJAX 调用。
以下代码示例是一个通用的 jQuery AJAX 数据库调用,可用于连续执行任意数量的数据库调用。它假定数据库有一个使用 JSON 响应的 HTTP POST API,因为您没有指定数据库使用的数据访问方法。
myfunction 参数指定数据库响应后将调用的函数。
myerrorfunction 参数指定错误处理程序。
myquery 参数指定 API 调用。
myparams 参数是要作为 URL 中的参数提供给 HTTP POST 的任意数量的逗号分隔参数的列表。它们采用 &p0=..&pn=
格式
function apitofunction6(myfunction,myerrorfunction,myquery,myparams) {
var datstr = "";
var indx = 0;
var ajaxHandle;
if (myparams != null) {
for (indx = 0; indx < (arguments.length-3); indx++) {
datstr = datstr + "&p" + indx + "=" + encodeURIComponent(arguments[indx+3]);
}
}
ajaxHandle = $.ajax({
type: "POST",
url: "aqapi",
data: "howsmyapiq=" + myquery + datstr,
dataType: "json",
tryCount: 0,
retryLimit: 3,
complete: function sortdata() {},
success: function(myJSON,textStatus,jqXHR) {
myfunction(myJSON,textStatus,jqXHR);
},
error: function(jqXHR, textStatus, errorThrown) {
if (textStatus == 'timeout') {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
var retryHandle = $.ajax(this);
pendingFunction.push(retryHandle);
return retryHandle;
}
}
if (errorThrown == 'Authorization Required') {
window.location = "/login";
}
myerrorfunction(jqXHR, textStatus, errorThrown);
}
});
pendingFunction.push(ajaxHandle);
return ajaxHandle;
}
函数可以按如下顺序调用:
arResultIndex = apitofunction6(pc_myOnGet,pc_myOnFail,"ShowIncident3",SortOrder,Count,Offset);
function pc_myOnGet (myData,myTS,myJqXHR,datstr,myparams) {
//format and display data
//...
arResultIndex2 = apitofunction6(pc_myOnGet2,pc_myOnFail2,"ShowIncident4",SortOrder,Count,Offset);
}
function pc_myOnGet2 (myData,myTS,myJqXHR,datstr,myparams) {
//format and display data
//...
arResultIndex3 = apitofunction6(pc_myOnGet3,pc_myOnFail3,"ShowIncident5",SortOrder,Count,Offset);
}