我在下面有下面的代码。我随意跑过去,如果我将这条警报消息确切地放在了它的位置,它就会工作。如果我把它拿出来或将它移到任何其他地方,标签将不会出现。使用jQuery处理JSON数据 - 需要alert的奇怪结果()
准确的警报是什么让代码能够工作,我该如何使它在没有警报的情况下工作?
$.ajax({
type: "GET",
url: "../ajax.php",
data: "action=tabs",
dataType: "json",
success: function(Projects){
$.each(Projects, function(i){
/* Sequentially creating the tabs and assigning a color from the array: */
var tmp = $('<li><a href="#" class="tab green">'+Projects[i].name+'<span class="left" /><span class="right" /></a></li>');
/* Setting the page data for each hyperlink: */
tmp.find('a').data('page','../ajax.php?action=lists&projectID='+Projects[i].project_id);
/* Adding the tab to the UL container: */
$('ul.tabContainer').append(tmp);
});
}
});
alert("yes");
Ajax代码与此代码
$query = mysql_query("SELECT * FROM `projects` ORDER BY `position` ASC");
$projects = array();
// Filling the $projects array with new project objects:
while($row = mysql_fetch_assoc($query)){
$projects[] = $row;
}
echo json_encode($projects);
返回的数据重新调整JSON是非常小,速度非常快,所以我不认为这是问题。
正确答案。作为一个便笺,他可以通过设置async:false来使AJAX调用同步,但它带来了性能问题。 – 2010-06-11 23:04:01
太简单了。不能相信我没有想到这一点。谢谢! – getSurreal 2010-06-11 23:10:26