简体   繁体   English

刷新页面时如何从Controller获取数据?

[英]How to get data from a Controller when refreshing the page?

I am creating a dashboard where i need to update the notifications bar each time i refresh the page so that, when refreshing the page, the controller has to be executed and it will return data from the database to that page. 我正在创建一个仪表板,每次刷新页面时都需要在其中更新通知栏,以便在刷新页面时必须执行控制器,控制器会将数据从数据库返回到该页面。

What is the best way to do this? 做这个的最好方式是什么? I've tried some stuff with the onload event but i cant seem to find a way of getting into the controller with it 我已经尝试了onload事件的一些东西,但我似乎找不到找到进入它的控制器的方法

Currently im trying: Jquery/Ajax: 目前正在尝试:Jquery / Ajax:

 $( document ).ready(function() {
   $.ajax({
    type: 'POST',
    url:'<?=base_url('notificacoes') ?>'
   }).done(function(e){
    console.log(e);
    $('#notbadge').html(e);
   }) 
});

Controller: 控制器:

<?php

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Notificacoes extends CI_Controller {
        function __construct(){
            parent::__construct();
            $this->load->model('Notificacoes', '', TRUE);
            $this->load->model('Viatura', '', TRUE);
            $this->load->library('session');
        }

        public function index(){
            $matricula = $this->session->viatura;
            $viatura = $this->Viatura_model->read($matricula);

            return $viatura;

        }
    }

HTML badge: HTML徽章:

<span class="badge" id="notbadge"></span>

It doesn't print me the array on the console, why? 它没有在控制台上显示我的阵列,为什么?

To get notification refereshing a page each time is not a good idea you should use javascript or angular js for this. 要使通知每次都刷新页面不是一个好主意,您应该为此使用javascript或angular js。 try something like this. 尝试这样的事情。

$scope.getNotifications = function () {
        $.ajax({
            url: "<?php echo base_url() . "dashboard/getNotifications" ?>",
            type: 'POST',
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                //console.log(data);
                $scope.notifications = data;
                $scope.total_unread_noti = data.total_unread_noti;
                $scope.$apply();
            }

        });
    };
    $scope.getNotifications();


    setInterval(function () {
        $scope.getNotifications();
    }, 3000);

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

相关问题 如何在不刷新整个页面的情况下从其他页面获取数据 - how to get data from different page with out refreshing the whole page 选择时获取下拉值,并将其传递给控制器​​,而无需刷新页面 - Get dropdown value when selected and pass it to controller without refreshing the page 如何在不刷新页面的情况下获取表单数据 - How to get the form data without refreshing the page 如何在不使用 GET 数据中的数据刷新页面而不单击按钮的情况下显示 mysql 数据库 - How to display a mysql database without refreshing the page with data from GET data without clicking button 如何在不刷新 2 个或多个用户页面之间的页面的情况下获取数据 - How to get data without refreshing page between 2 or more user page 如何保存过滤器数据并且在刷新页面时不会丢失 - how to save filters data and don't get lost when refreshing page 如何使用Ajax和Php从数据库获取实时数据而无需刷新页面? - How to get real-time data from database without page refreshing using Ajax and Php? 当前数据是从网页上的tcp获取的,而不刷新整个页面? - Present data get from tcp on webpage without refreshing entire page? 防止zend控制器/ ajax刷新页面 - Preventing zend controller/ajax from refreshing the page 如何从Joomla中的输入页面获取数据并在控制器中使用它 - How to get data from an input page in Joomla and use it in a controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM