dtdr84101 2017-05-23 08:47
浏览 35
已采纳

在codeigniter中使用javascript刷新div

I have the following file named navbar.php. and I wanted it to be refreshed with a time delay so I used the js function bellow and it works fine. problem is when page refreshed it doesnt go to the actual path, instead it adds the name of the controller and function to the source path and return 404 , help pls.

<div class="group" id="mydiv">
<!--some content -->
</div>

<script type="text/javascript">
$(document).ready(function () {
    setInterval(function() {
        $.get("application/views/templates/navbar.php", function (result) {
            $('#mydiv').html(result);
        });
    }, 15000);
});

Chrome console log says : GET http://localhost/HDMSV2/dashbord/Dashbord_Controller/application/views/templetes/navbar.php 404 (Not Found)

  • 写回答

1条回答 默认 最新

  • douyuan5600 2017-05-23 08:50
    关注

    Try domain relative path to php file

    $(document).ready(function () {
    setInterval(function() {
        $.get("/application/views/templates/navbar.php", function (result) {
            $('#mydiv').html(result);
        });
    }, 15000);
    });
    

    But I think you need to go to some controller and create function there that will return you proper values, because it won't make too much sense accessing view file if it isn't populated with data from controller as that is how MVC pattern should work

    So in your controller RefreshController create function refresh_navbar()

    public function refreshNavbar() {
        $this->load->view('navbar');
    }
    

    And then your script would go something like this

    $(document).ready(function () {
    setInterval(function() {
        $.get("/refresh/refresh-navbar", function (result) {
            $('#mydiv').html(result);
        });
    }, 15000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏