【问题标题】:Ajax request after page load页面加载后的 Ajax 请求
【发布时间】:2011-12-04 13:05:22
【问题描述】:

我希望能够使用 jQuery AJAX 从数据库中刷新或获取更新的记录。 无需刷新页面。

这是诀窍,既然有人会发表评论,我怎么能调用 Ajax 请求。因为该评论可以随时发布。

现在我只是在页面加载时加载记录。我希望能够在页面已经加载并发表评论时加载记录。

我只想要一个简单的解决方案,这个项目不适合生产。这只是我正在进行的一个学校项目。

我想到了每 20 秒一次的 Ajax 请求,或者可能在用户 cmet 时调用一个更新函数。

【问题讨论】:

  • @asdgfassagas 然后将其添加到您的问题中并使其具体化。

标签: php jquery mysql ajax


【解决方案1】:

这样做。

<script language="javascript">

//function that refresh the comment list
function load_new_comments() {
    //jquery ajax call to pull the new records and update the comment area
}

//function to add a comment into dataase
function add_new_comment() {
    //jquery ajax call to add the new records 
   load_new_comments();
}

//callback function.refresh time set to 30 seconds.
function callback() {
    setTimeout("pollingFunction();",30000);
}

//refresh function
function pollingFunction() {
    load_new_comments();
    callback();
}

$(document).ready(function() {
     pollingFunction();//initiating the function for the 1st time.
 });

</script>

【讨论】:

    【解决方案2】:

    很简单,使用 javascript 发布 cmets,在每次向服务器发出 javascript 请求后,执行页面刷新请求以获取较新的 cmets 和帖子,在您的标记中相应地分发它们,然后您可以再次使用 setInterval 执行此操作在第二个基础上。

    【讨论】:

      【解决方案3】:
      var auto_refresh_comments = setInterval(
      function () {
      $('#comments').load('reload_comments.php?meeting='+ meeting_id+'').fadeIn("slow");
      }, 5000); // refresh every 5000 milliseconds
      

      每 5 秒后重新加载 #comment 元素。

      【讨论】:

        最近更新 更多