在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)

douchuifk90315
douchuifk90315 你不能直接调用视图(可能你可以,但是真的很糟糕)。您必须调用控制器并从中加载导航栏视图。
3 年多之前 回复
dourang20110122
dourang20110122 它找到了该文件但开始返回内部服务器error.localhost/HDMSV2/application/views/templates/navbar.ph-<p<500(内部服务器错误)
3 年多之前 回复
douke6424
douke6424 使用相对于域的路径,以/开头。
3 年多之前 回复

1个回答

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);
doudi5524
doudi5524 对不起,道歉。 它确实刷新但开始返回'不允许直接脚本访问'。
3 年多之前 回复
douxi3554
douxi3554 我试过它,它根本不重新加载/刷新。 没有调试通知,没有更改。
3 年多之前 回复
dongsu2807
dongsu2807 你能举个例子怎么做吗?
3 年多之前 回复
duanjianl183188
duanjianl183188 当然,因为您无法直接访问视图。 而是创建函数refreshNavbar(),它将加载视图navbar.php并传递正确的数据以正确呈现,然后您可以通过ajax获取该html
3 年多之前 回复
dongyou2714
dongyou2714 它找到了该文件,但开始返回内部服务器error.localhost / HDMSV2 / application / views / templates / navbar.php 500(内部服务器错误)
3 年多之前 回复
立即提问
相关内容推荐