0

我遇到了一个问题,我即将伤害 MVC 范式,所以我宁愿问你该怎么做。我得到了一个使用 jQuery .post() 方法每 10 秒刷新一次的页面。

setInterval(function() {
    $.post("http://XYZ.de<?php echo $this->webroot."Posts/index"; ?>", { liveUpdate: "true" },
        function(response) {
            $('#loadingContent').html(response);
        }
    );
}, 10000);

现在,放置“帖子/索引”的位置我必须调用 Cake 的 PostsController.php,它允许我重置变量。

但它不是那样工作的,响应中充满了正常页面调用的所有 html,但我只想更新纯 PHP 变量,而不将 html 附加到该 div。

我究竟做错了什么?提前感谢您的耐心等待。

4

1 回答 1

0

由于您的数组是复杂的多级数组并且您不想解析 JSON,因此我认为这样做的唯一方法是使用 jQuery load()。

setInterval(function() {
  jQuery("#RefreshMeOnPage")
    .load("http://XYZ.de<?php echo $this->webroot."Posts/index"; ?> #RefreshMeInResults > *", {liveUpdate: "true"});
  }, 10000);

它将向服务器发出 post 请求,并用RefreshMeOnPage新收到的 id 元素的内容替换现有元素的内容 id RefreshMeInResults。文档http://api.jquery.com/load/

注意:仍然刷新率为 10 秒,我认为您应该研究 ajax comet 解决方案http://cometdaily.com/maturity.html。它只会在发生变化时接收数据,尽管它需要一些配置。

于 2013-04-04T20:07:47.533 回答