dongxun6690 2016-10-18 17:35
浏览 50

在codeigniter中使用jQuery ajax方法获取数据

Below is my view:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="<?php echo base_url("assets/css/bootstrap.min.css"); ?>">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
        <script src="<?php echo base_url("plugins/jQuery/jQuery-2.2.0.min.js"); ?>"></script>
        <script type="text/javascript" charset="utf-8">
            function waitForMsg1() {
                $.ajax({//Ajax method to get data from Message Controller
                    type: "GET",
                    url: "<?php echo base_url('index.php/message/messageCount'); ?>",
                    async: true,
                    cache: false,
                    timeout: 50000,
                    success: function (data) {
                        addmsg("new", data);
                        setTimeout(waitForMsg1, 1000);
                    },
                });
            };

            $(document).ready(function () {
                waitForMsg1();
            });

            function addmsg(type, count) {
                $('#badge').html(count);  //selecting the element to display inside the badge
            }
        </script>

    </head>
    <header>
        <a href="#"><span><div id="bounce"><i class="fa fa-envelope ">
                        <span class="badge" id="badge" style="background-color:red;margin:-25px 0 0 -5px;">
                        </span></i></div></span></a>
    </header>

Below is the Controller:

<?php
class Message extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('MessageDb');
        $this->load->model('UserDb');
        $this->load->model('EmployeeDb');
    }

    public function messageCount()
    {
        echo '12345';   //This is the dummy value.
    }

}
?>

I want to display the count from Message controller into the view, it is not working. I tried a lot but not able to rectify the problem. How this can be done?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记