Ajax-скрипт не загружает ничего и не отправляет запросы - PullRequest
1 голос
/ 14 апреля 2011

Этот скрипт должен запускать JQuery Ajax-скрипт каждую секунду, он должен получать все новые сообщения, а затем помещать их поверх оригинальных.Но в данный момент ничего не происходит, загружаются начальные сообщения, но больше ничего.Консоль Firebug не сообщает ни об ошибках, ни об 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
}
?>

Ответы [ 2 ]

0 голосов
/ 14 апреля 2011

Часть кода в начале выглядит неправильно. Это должно работать. Он будет вызывать функцию oneSecondFunction каждую секунду:

$(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 голосов
/ 14 апреля 2011

Я процитировал раздел, в котором он получает идентификатор, потому что «это» ни к чему не относилось.

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...