views:

63

answers:

1

As it stands, I have some JQuery along these lines -

ph=function(i){
$.each(i,function(po){
id=po.id;url="/"+id;t=$('<div id='+ id +' class=entry></div>');
t.html('<div class=hx><a href="/users/'+ po.author +'">'+ po.author +'</a></div><div class=b>'+ po.body +'</div>');
t.prependTo($("#modpskx"));
t.hide();
t.slideDown("slow");});};
function mtoxps(){
mid=$("#xsodl").val();
id=$("#modpskx > div:first")[0].id;
if(id==""){id=0}
$.ajax({
url:'/widgets/live.php',
data:{'id':id,'mid':mid},
type:'post',
dataType:'json',
cache:false,
success:function(b){ph(b);}});}
setInterval("mtoxps()", 10000);

The JSON data sent back to the browser by the server is as follows:

[{"author":"NiX","timestamp":"2009-12-20 21:35:32","id":"194","mig_id":"3","body":"Cows.","ip":""}]

This - every 10 seconds - successfully requests any new results. Unfortunately, the "ph" variable (what is used to return and display the result(s)) isn't properly parsing the returned output. Instead, it displays all the variables as undefined, resulting in the next request to send the server an "undefined" variable, as well.

Are there any suggestions as to why this isn't functioning?

+2  A: 

Try:

$.each(i, function(index, po){

instead of

$.each(i, function(po){

The callback for the each function is at the bottom of this page.

brianng
Looks like this worked. Not entirely sure what the magic behind it is (I'll need to read the documentation that you linked), but it did the job! Thanks. :)
NiX
NiX was using $.each(). The doc referenced by brianng says "This function [$.each] is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything."
jdigital
NiX, your implementation would have worked if you changed po in the each function to `this`. po would refer to the index in the array.
Jim Schubert