简体   繁体   English

Codeigniter和AJAX-Div重新加载不起作用

[英]Codeigniter & AJAX - Div reload not working

I am able to display the text straight from database on a page. 我可以直接在页面上显示数据库中的文本。 The thing is that when the text is edited in database, I want it to instantly reload and display the new text without reloading the page. 事实是,当在数据库中编辑文本时,我希望它立即重新加载并显示新文本,而无需重新加载页面。

I am not able to make that happen and I don't know why it's not wokring. 我无法做到这一点,而且我也不知道为什么它不起作用。 I want it to reload every 2 seconds. 我希望它每2秒重新加载一次。

My view file 我的检视档案

<div id="results"></div>

<script>
        $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html);
            },
           Timeout:2000
    });
</script>

My controller 我的控制器

 public function get() {

    $this->load->model('editor_model');
    $result = $this->editor_model->get();
    if ($result) {
        echo $result->body;

    }
}

It displays it but doesn't reload. 它显示它,但不重新加载。 Please help. 请帮忙。 Thanks! 谢谢!

you need to use setInterval() 您需要使用setInterval()

<script>
 $(document).ready(function(){
   var interval = function(){
        $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html); // you can use .append(html) instead of .text(html) to append new data to div
            },
           Timeout:2000
       });
    });
    //var get_new_data = setInterval(interval , 5000); 
    setInterval(interval , 5000);// it will refresh ajax every 5 seconds you can use a previous line if you need to stop interval by using clearInterval(get_new_data);
});
</script>

Timeout is wrong to use like that 这样使用Timeout是错误的

Try like this, 这样尝试

var i = setInterval(function(){
     $.ajax({
            url: "<?php echo base_url('home/get') ?>",
            cache: false,
            success: function (html) {
                $("#results").text(html);
            },
    });
},2000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM