Jquery 当所有ajax请求完成时,然后
我需要执行一系列ajax请求(我需要这样做来填充一些选择框),并且在加载所有内容之后,隐藏加载面板 如果我这样做的话,效果会非常好:Jquery 当所有ajax请求完成时,然后,jquery,ajax,jquery-deferred,Jquery,Ajax,Jquery Deferred,我需要执行一系列ajax请求(我需要这样做来填充一些选择框),并且在加载所有内容之后,隐藏加载面板 如果我这样做的话,效果会非常好: $.when( $.getJSON(applicationUrl + "data/codiciPaese.json", function (json) { data.codiciPaese = json; $.each(data.codiciPaese, function (index, item) {
$.when(
$.getJSON(applicationUrl + "data/codiciPaese.json", function (json) {
data.codiciPaese = json;
$.each(data.codiciPaese, function (index, item) {
$(".codiciPaese").append($('<option>', {
value: item.code,
text: item.code + " - " + item.name
}));
});
}),
$.getJSON(applicationUrl + "data/regimiFiscali.json", function (json) {
data.regimiFiscali = json;
$.each(data.regimiFiscali, function (index, item) {
$(".regimiFiscali").append($('<option>', {
value: item.code,
text: item.code + " - " + item.name
}));
});
}),
$.Deferred(function (deferred) {
$(deferred.resolve);
})
).done(function () {
$('#loading').fadeOut(800, function () {
// something else
});
});
$。什么时候(
$.getJSON(applicationUrl+“data/codiciPaese.json”,函数(json){
data.codiciPaese=json;
$。每个(数据。编码,功能(索引,项目){
$(“.codiciPaese”)。追加($(''){
值:item.code,
文本:item.code+“-”+item.name
}));
});
}),
$.getJSON(applicationUrl+“data/regimiFiscali.json”,函数(json){
data.regimiFiscali=json;
$.each(data.regimiFiscali,函数(索引,项){
$(“.regimiFiscali”)。附加($(''){
值:item.code,
文本:item.code+“-”+item.name
}));
});
}),
$.Deferred(函数(延迟){
$(延期。解决);
})
).done(函数(){
$('#加载')。淡出(800,函数(){
//别的
});
});
但是,由于我想采用DRY原则:)并且将有两个以上的请求,因此我尝试将getJSON请求包装到每个周期中:
var elementsToGet = { // I'll reuse this to get the values elsewhere
codiciPaese: "HY",
regimiFiscali: "RF01",
};
$.when(
$.each(elementsToGet, function (element, defaultValue) {
$.getJSON(applicationUrl + "data/"+element+".json", function (json) {
data.element = json;
$.each(data.element, function (thisIndex, thisElement) {
$(parent + " ."+element).append($('<option>', {
value: thisElement.code,
text: thisElement.code + " - " + thisElement.name
}));
});
});
}),
$.Deferred(function (deferred) {
$(deferred.resolve);
})
).done(function () {
$('#loading').fadeOut(800, function () {
// something else
});
});
var-elementsToGet={//我将重用它以在其他地方获取值
附录:“HY”,
regimiFiscali:“RF01”,
};
美元。什么时候(
$.each(elementsToGet,函数(element,defaultValue){
$.getJSON(applicationUrl+“data/”+元素+”.json),函数(json){
data.element=json;
$.each(data.element,function)(thisIndex,thisElement){
$(父元素+“+”元素)。追加($(“”{
值:thisElement.code,
文本:thisElement.code+“-”+thisElement.name
}));
});
});
}),
$.Deferred(函数(延迟){
$(延期。解决);
})
).done(函数(){
$('#加载')。淡出(800,函数(){
//别的
});
});
问题是,这种“延迟”方式不再使用,因此在加载所有数据之前,加载面板将消失。我做错了什么?这个怎么样
var functionToCall = 2;
var functionCalled = 0;
$.getJSON(applicationUrl + "data/codiciPaese.json", function (json) {
data.codiciPaese = json;
$.each(data.codiciPaese, function (index, item) {
$(".codiciPaese").append($('<option>', {
value: item.code,
text: item.code + " - " + item.name
}));
});
AmIDone();
});
$.getJSON(applicationUrl + "data/regimiFiscali.json", function (json) {
data.regimiFiscali = json;
$.each(data.regimiFiscali, function (index, item) {
$(".regimiFiscali").append($('<option>', {
value: item.code,
text: item.code + " - " + item.name
}));
});
AmIDone();
});
function AmIDone() {
functionCalled++;
if (functionCalled == functionToCall) {
$('#loading').fadeOut(800, function () {
// something else
});
}
}
var functionToCall=2;
var-functionCalled=0;
$.getJSON(applicationUrl+“data/codiciPaese.json”,函数(json){
data.codiciPaese=json;
$。每个(数据。编码,功能(索引,项目){
$(“.codiciPaese”)。追加($(''){
值:item.code,
文本:item.code+“-”+item.name
}));
});
酰胺酮();
});
$.getJSON(applicationUrl+“data/regimiFiscali.json”,函数(json){
data.regimiFiscali=json;
$.each(data.regimiFiscali,函数(索引,项){
$(“.regimiFiscali”)。附加($(''){
值:item.code,
文本:item.code+“-”+item.name
}));
});
酰胺酮();
});
功能酰胺酮(){
函数名为++;
if(functionCalled==functionToCall){
$('#加载')。淡出(800,函数(){
//别的
});
}
}
我认为您可以向$提供多个承诺。当
:
var promises = $.map(elementsToGet, function(element, defaultValue) {
return $.getJSON(applicationUrl + "data/" + element + ".json", function(json) {
$.each(data.element, function(thisIndex, thisElement) {
$(parent + " ." + element).append($('<option>', {
value: thisElement.code,
text: thisElement.code + " - " + thisElement.name
}));
});
});
});
$.when.apply(null, promises).done(function() {
$('#loading').fadeOut(800, function() {
// something else
});
});
var promises=$.map(elementsToGet,函数(element,defaultValue){
返回$.getJSON(applicationUrl+“data/”+元素+“.json”,函数(json){
$.each(data.element,function)(thisIndex,thisElement){
$(父元素+“+”元素)。追加($(“”{
值:thisElement.code,
文本:thisElement.code+“-”+thisElement.name
}));
});
});
});
$.when.apply(空,承诺).done(函数()){
$(“#加载”).fadeOut(800,function(){
//别的
});
});
注意,
.apply(null,promises)
之所以出现在这里,是因为$。当期望延迟对象作为$传递时。当(d1,d2,d3)
而不是数组时。如果我得到了您的问题,您需要在ajax请求之后调用ajax请求并执行一些操作。
在这里你会有一个大致的想法。您可以根据需要进行更改
//The data you want to send to server side
var dt1={
dt1:val1,
dt2:$("#txt_EmailLogin").val()
};
//Your ajax-1 request.
var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
url: "yourServer.php",
type: "POST",
data: dt1,
dataType: "json"
});//End of request-1
//Ajax-1 Done catch JSON from PHP
request.done(function(dataset){
for (var index in dataset){
dataPHPtoJsJS=dataset[index].dataPHPtoJs;
asManyasYouWantJS=dataset[index].asYouWant;
}
//JavaScript conditions. Here you can control the behaivior of your html object, based on your PHP response. HERE YOU CAN HIDE YOUR LOAD PANNEL - IF IS THE CASE.
if(dataPHPtoJsJS){
$( "#idYourHtmlElement" ).removeClass( "class1" )
$( "#idYourHtmlElement" ).addClass( "class2" )
}
/////////////////////////////////////////////////////////////
//Here will be your second Ajax that will be trigged after the Ajax-1 will done. Only repeat everything
var dt2={
dt3:val1,
dt4:$("#txt_EmailLogin").val()
};
//Your ajax-2 request.
var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
url: "yourServer2.php",
type: "POST",
data: dt2,
dataType: "json"
});
request.done(function(dataset){
for (var index in dataset){
dataPHPtoJsJS=dataset[index].dataPHPtoJs;
asManyasYouWantJS=dataset[index].asYouWant;
}//End of for
//Here you can call another Ajax AJAX-3, you can controll your HTML elements - HERE YOU CAN HIDE YOUR LOAD PANNEL - after the second AJAX is done
});//End of DONE-2
//Ajax-2 Fail
request.fail(function(jqXHR, textStatus) {
alert("Request -1 failed: " + textStatus);
});
/////////////////////////////////////////////////////////////
});//End of AJAX-1
//Ajax-1 Fail
request.fail(function(jqXHR, textStatus) {
alert("Request -1 failed: " + textStatus);
});
尝试将第一个$更改为$.map
,并在$.getJSON
前面添加return
?和data.element=json代码>应该不是必需的。除此之外,这个解决方案看起来还不错。将变量promises
命名为$更容易理解。map(…)
只返回一个承诺数组,而不是一个延迟。事实上,您是对的。重命名以保持一致性。