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

时间:2016-10-18 17:35:13

标签: php jquery ajax codeigniter

以下是我的观点:

<!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>

以下是控制器:

<?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.
    }

}
?>

我想将消息控制器中的计数显示到视图中,它不起作用。我尝试了很多,但无法解决问题。如何做到这一点?

0 个答案:

没有答案