我正在尝试创建几个需要等待来自维基百科的getJSON请求的html元素。我是这样做的:
$.each(urls, function (i, url) {
jxhr.push(
$.getJSON(url, function (json) {
alert("inside: " + i);
wikiHTML = json.parse.text["*"];
$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
pages += '<div class="f-page">';
pages += '<div class="f-title">';
pages += '<a href="index.jsp">Back to bookshelf</a>';
pages += '<h2 id="event_name">Your Interests</h2>';
pages += '<a href="#"></a>';
pages += '</div>';
pages += '<div class="box w-50 h-100">';
pages += '<div class="img-cont img-1"></div>';
pages += '<h3>' + interests[i].name + '</h3>';
pages += $wikiDOM.find('.infobox').html();
pages += '</div>';
})
);
});
$.when.apply($, jxhr).done(function() {
alert("inside last");
result_html += pages;
result_html += '<div class="f-page f-cover-back">';
result_html += '<div id="codrops-ad-wrapper"><a href="index.jsp" >Back to bookshelf</a></div>';
result_html += '</div>';
$('.container').append(result_html);
});
问题是它永远不会进入$.when.apply
..这是因为请求永远不会完成?如果getJSON中的一个请求失败会有问题吗?
我也在使用 jquery 1.10.2
这个代码也在里面:
$(document).ajaxStop(function() {
});
功能
答案 0 :(得分:0)
尝试同步$.each()
循环中的调用,然后在.done()
函数后执行代码:
$.each(urls, function (i, url) {
$.ajax({
url: url,
type: 'GET',
async: false,
success: function (json) {
alert("inside: " + i);
wikiHTML = json.parse.text["*"];
$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
pages += '<div class="f-page">';
pages += '<div class="f-title">';
pages += '<a href="index.jsp">Back to bookshelf</a>';
pages += '<h2 id="event_name">Your Interests</h2>';
pages += '<a href="#"></a>';
pages += '</div>';
pages += '<div class="box w-50 h-100">';
pages += '<div class="img-cont img-1"></div>';
pages += '<h3>' + interests[i].name + '</h3>';
pages += $wikiDOM.find('.infobox').html();
pages += '</div>';
}
});
});
alert("inside last");
result_html += pages;
result_html += '<div class="f-page f-cover-back">';
result_html += '<div id="codrops-ad-wrapper"><a href="index.jsp" >Back to bookshelf</a></div>';
result_html += '</div>';
$('.container').append(result_html);
旧答案
尝试将.done()
函数直接添加到$.getJSON()
调用(就像jQuery documentation中的示例中所做的那样):
$.each(urls, function (i, url) {
jxhr.push(
$.getJSON(url, function (json) {
alert("inside: " + i);
wikiHTML = json.parse.text["*"];
$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
pages += '<div class="f-page">';
pages += '<div class="f-title">';
pages += '<a href="index.jsp">Back to bookshelf</a>';
pages += '<h2 id="event_name">Your Interests</h2>';
pages += '<a href="#"></a>';
pages += '</div>';
pages += '<div class="box w-50 h-100">';
pages += '<div class="img-cont img-1"></div>';
pages += '<h3>' + interests[i].name + '</h3>';
pages += $wikiDOM.find('.infobox').html();
pages += '</div>';
}).done(function() {
alert("inside last");
result_html += pages;
result_html += '<div class="f-page f-cover-back">';
result_html += '<div id="codrops-ad-wrapper"><a href="index.jsp" >Back to bookshelf</a></div>';
result_html += '</div>';
$('.container').append(result_html);
});
);
});