2011-04-14 122 views
1

這個腳本應該每秒運行一次jQuery Ajax腳本,它應該獲取所有較新的帖子,然後將它們放在原始腳本之上。但目前沒有任何事情發生,它加載了最初的帖子,但沒有別的。 Firebug控制檯報告沒有錯誤,或根本沒有發送Ajax請求。你能看到這個腳本有什麼問題嗎?謝謝:)Ajax腳本沒有加載任何內容,也沒有發送請求

源代碼 - 的index.php(我離開了CSS):

<?php 
    include('config.php'); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Twitter Style load more results.</title> 
<link href="frame.css" rel="stylesheet" type="text/css"> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/ 
libs/jquery/1.3.0/jquery.min.js"></script> 

<script type="text/javascript"> 
$(function() { 
    $(function() { 
     setInterval(oneSecondFunction, 1000); 
    }); 

    function oneSecondFunction() { 
     var ID = $(this).attr("id"); 
     if (ID) { 
      $("#more" + ID).html('<img src="moreajax.gif" />'); 

      $.ajax({ 
       type: "POST", 
       url: "ajax_more.php", 
       data: "lastmsg=" + ID, 
       cache: false, 
       success: function (html) { 
        $("ol#updates").prepend(html); 
        $("#more" + ID).remove(); 
       } 
      }); 
     } else { 

     } 


     return false; 


    }; 
}); 
</script> 
</head> 
<body> 
<div id='container'> 
    <ol class="timeline" id="updates"> 
<?php 
    $sql=mysql_query("select * from updates ORDER BY item_id DESC LIMIT 9"); 
    while($row=mysql_fetch_array($sql)) 
    { 
     $msg_id=$row['item_id']; 
     $message=$row['item_content']; 
?> 
<li> 
    <?php echo $message; ?> 
</li> 
<?php } ?> 
</ol> 
</div> 
</body> 
</html> 

ajax_more.php -

<?php 
include("config.php"); 


if(isset($_POST['lastmsg'])) 
{ 
    $lastmsg=$_POST['lastmsg']; 
    $result=mysql_query("select * from updates where item_id<'$lastmsg' order by item_id desc limit 9"); 
    $count=mysql_num_rows($result); 
    while($row=mysql_fetch_array($result)) 
    { 
     $msg_id=$row['item_id']; 
     $message=$row['item_content']; 
?> 


<li> 
    <?php echo $message; ?> 
</li> 


<?php 
    } 


?> 
<?php 
} 
?> 
+0

您是否設置了斷點並遍歷代碼? – Tod 2011-04-14 19:02:27

+0

我已經通過它分配。我對jQuery不太瞭解。 – user663049 2011-04-14 19:05:03

+0

我沒有看到實際功能被調用的地方。 – Munzilla 2011-04-14 19:07:39

回答

0

一開始的代碼看起來不對。這應該工作。它會每秒調用一次函數:

$(document).ready(function() { 

    setInterval(oneSecondFunction, 1000); 


    function oneSecondFunction() { 
    //console.log('run oneSecondFunction'); 
    var ID = $(this).attr("id"); 
    if(ID) { 
     $("#more"+ID).html('<img src="moreajax.gif" />'); 
     $.ajax({ type: "POST", url: "ajax_more.php", data: "lastmsg="+ ID, cache: false, 
       success: function(html){ 
       $("ol#updates").prepend(html); 
       $("#more"+ID).remove(); 
       } 
       }); 
    } else { 
    } return false; }; 
}); 
0

我引用了那裏得到的ID部分,因爲'這'並不是指任何東西。

<script type="text/javascript"> 
$(function() { 
    setInterval(oneSecondFunction, 1000); 
}); 

function oneSecondFunction() { 
    //var ID = $(this).attr("id"); 
    var ID = '1'; 
    if(ID){ 
     $("#more"+ID).html('<img src="moreajax.gif" />'); 
     $.ajax({ 
      type: "POST", 
      url: "ajax_more.php", 
      data: "lastmsg="+ ID, 
      cache: false, 
      success: function(html){ 
       $("ol#updates").prepend(html); 
       $("#more"+ID).remove(); 
      } 
     }); 
    } 
    return false; 
} 
</script> 
相關問題