我创建了一个JavaScript函数,它使用AJAX来调用CodeIgniter控制器函数,该函数调用模型并从数据库中检索数据并以json形式返回。
我已成功创建它并使用jQuery Template插件呈现数据,但是当我移动View时会遇到错误,这会将index()函数中的数据呈现为不同的函数。
ajax出错,状态为200。
下面是代码:
<?php
class Scheduler extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Scheduler_model');
}
public function index() {
$this->load->view('templates/header');
$this->load->view('Rooms_view'); //data appears/gets rendered here
$this->load->view('templates/footer');
}
public function rooms() {
$this->load->view('templates/header');
$this->load->view('Rooms_view'); //error is encountered here
$this->load->view('templates/footer');
}
public function read($table) {
echo json_encode( $this->Scheduler_model->getAll($table) );
}
的JavaScript / jQuery的:
var readUrl = 'scheduler/read',
createUrl = 'scheduler/create',
updateUrl = 'scheduler/edit',
delUrl = 'scheduler/delete',
delHref,
delId,
updateHref,
updateId;
$(document).ready(function() {
var pathname = window.location.pathname;
if (pathname == '/MobileSchedule/scheduler/rooms'){ read('rooms'); }
//if i call read("rooms") here, it appears on index(), but errors on rooms()
}); //end Document ready
function read(table) {
$( '#ajaxLoader' ).fadeIn( 'slow' );
$.ajax({
url: readUrl + '/' + table,
dataType: 'json',
success: function( response ) {
for( var i in response ) {
response[ i ].updateLink = updateUrl + '/' + response[ i ].id;
response[ i ].deleteLink = delUrl + '/' + response[ i ].id;
}
//clear old rows
$( '.roomsList' ).html( '' );
//append new rows
$( '#roomsTemplate' ).render( response ).appendTo( '.roomsList' );
$( '#ajaxLoader' ).fadeOut( 'slow' );
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr);
console.log(thrownError);
}
});
}