[英]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.