迭代json列表时出现未定义的错误

时间:2010-08-01 13:35:41

标签: javascript jquery json

我得到了这个代码,继续返回未定义的msg而不是预期的html。 功能目的: 以下功能的目的是返回类似于fb one的通知。代码工作正常。但是我无法弄清楚getJSON部分有什么问题。因此,不是返回“clonex1喜欢你的帖子”,我得到了未定义。 代码

function buzzme()
{
jQuery('#cnumber').removeClass();
jQuery('#cnumber').empty();

jQuery('#floating_box').toggle();

 var nHeader = '<div id="floating_box" class="fb">' +
'<div id="header"><span id="htext">Notifications</span></div>' +
'<div id="int">' +
'<div id="bodyx">' +
'<ul>';
var nFooter = '</ul>' +
'<div class="jfooter">' +
'<a href="#" id="seemore">See all notifications</a>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var nContent;

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })

});

alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
}

notifications.php - setUpFlayout();和setUpJSONList()

    function setUpFlyout() {
    $notify = new se_notify();
    $data2 = $notify->notify_summary();
    $trk = 0;

    if($data2['total'] >= 1) {
    for($i = 0; $ $i <= $data2['total']; $i++) {
$nid = $data2['notifys'][$i]['notify_id'];
$im = $data2['notifys'][$i]['notify_icon'];
$img = "<img src='./images/icons/$im' />";
$notifier = $data2['notifys'][$i]['notify_text'][0];
$atype = $data2['notifys'][$i]['notifytype_id'];
$url = '';
$url2 = $data2['notifys'][$i]['notify_url'];
if($atype == 1) {
  $url = ' has sent you friend <a href='.$url2.'>request</a>';  
}
$trk++;    
if($data2['total'] >= 2) {
    $ret_arr = '';
     if($i == 0) {
         $ret_arr = '[';
     }
     $ret_arr = $ret_arr.setUpJSONList($data2['total'], $nid, $img, $notifier, $url, $trk);
     if($i == $data2['total']-1) {
         $ret_arr = $ret_arr.']';
     }
    echo ''; 
} else if($data2['total'] == 1){
   //$ret_arr = '[{"dh3":"'.$data2['total'].'","nid":"'.$nid.'", "img":"'.$img.'","notifier":"'.$notifier.'","url":"'.$url.'"}]';
   $ret_arr = '';
   echo $ret_arr;
}
    if($i == ($data2['total']-1))
        break;
        }
    }
}

setUpJSONList();

function setUpJSONList($total, $nid, $img, $notifier, $url, $track) {
    $comma = ',';
    $lp = '';
    $rp = ']';
    $result = '';
    if($track == $total) {
    $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"}';
    } else {
        $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"},';    
    }
    return $result;
}

感谢

1 个答案:

答案 0 :(得分:3)

getJSON之后对nContent的使用可能是未定义的,因为getJSON是异步的,并且不会完成初始化nContent。您需要在getJSON的回调中使用nContent移动代码。

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })
    alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
});