收到ajax响应时的通知更新

时间:2012-06-04 19:09:27

标签: javascript jquery ajax ejs

我有更新通知的方法,在更新的时候用json型返回通知的数量以及通知的内容

{
    "friend_request": 4,
    "request": [{
        "user_id": "1",
        "picture": "/home/sepdau/",
        "name": "Le Chanh"},
    {
        "user_id": "2",
        "picture": "",
        "name": "Yii PHP"},
    {
        "user_id": "4",
        "picture": "13366396884.jpg",
        "name": "Minh Le"},
    {
        "user_id": "11",
        "picture": "",
        "name": "Thang Phan"}]
}​

在收到通知成功的更新号码的时候

function updateNotification(){        
    $.ajax({
        url: '/nevergiveup/index.php/site/updatenotification',
        type: "post",
        dataType: "json",
        success: function(data){            
            if(data.friend_request>0){
                $(".zingcounter").text(data.friend_request); //update number of nofitcation
                // load the template file, then render it with data
                var html = new EJS({url: '/nevergiveup/jstemplates/friend_request.ejs'}).render(data);
                //$("#frlist").append(html);
                //$(html).replaceAll('#replacehere');
                $('#replacehere').replaceWith(html); // update content of notification
            } 
            setTimeout(updateNotification,10000);
        },
        error: function(){
            setTimeout(updateNotification,10000);
        }
    });       
}

我使用EJS构建内容模板
我有一个<div id="replacehere">来替换我的内容 我使用$('#replacehere').replaceWith(html);替换,但是在第一次请求后的10秒内成功 我看到json数据接收有一个新内容,通知数量有变化,但内容没有变化 如何在收到新内容时更改它。

1 个答案:

答案 0 :(得分:2)

猜猜你只需要使用

$('#replacehere').html(html);

否则它将删除#replacehere div ...并且第二个请求不会发现它将内容放入...

相关问题