Processing JSON data with jQuery - strange results needing alert()
Posted
by James
on Stack Overflow
See other posts from Stack Overflow
or by James
Published on 2010-06-11T22:50:43Z
Indexed on
2010/06/11
22:52 UTC
Read the original article
Hit count: 139
I have this code below. I randomly ran across that it will work if I have that alert message exactly where it is. If I take it out or move it to any other spot the tabs will not appear.
What exactly is that alert doing that allows the code to work and how can I make it work without the alert?
If I move the each loop into the success section it does not work even with the alert.
$.ajax({
type: "GET",
url: "../ajax.php",
data: "action=tabs",
dataType: "json",
success: function(data){
Projects = data;
}
});
alert("yes");
$.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);
});
The ajax code is retuning json with this code
$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);
The returning data is very small and very fast so I don't think that is the problem.
© Stack Overflow or respective owner