Javascript 基于当前路径名调用包含AJAX get的函数
我创建了一个JavaScript函数,该函数使用AJAX调用CodeIgniter控制器函数,该函数调用模型并从数据库检索数据,并作为json返回 我已经成功地创建了它,并使用jQuery模板插件呈现了数据,但是当我将呈现数据的视图从index()函数移动到另一个函数中时,我遇到了错误 ajax出错并给出200的状态 代码如下:Javascript 基于当前路径名调用包含AJAX get的函数,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我创建了一个JavaScript函数,该函数使用AJAX调用CodeIgniter控制器函数,该函数调用模型并从数据库检索数据,并作为json返回 我已经成功地创建了它,并使用jQuery模板插件呈现了数据,但是当我将呈现数据的视图从index()函数移动到另一个函数中时,我遇到了错误 ajax出错并给出200的状态 代码如下: <?php class Scheduler extends CI_Controller { public function __construct() {
<?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) );
}
你有什么错误?使用inspect元素中的network选项卡查看HTTP请求发送/接收的内容,这将有助于在console.log(xhr)上进行调试-它的readyState为4,status为200,responseText等于整个页眉、rooms_视图和页脚视图文件。对于thrownError log-message=“意外令牌,我查看了网络选项卡,发现“房间”的路径为“/MoibileSchedule/scheduler/scheduler/read”。它只应为“/MoibileSchedule/scheduler/read”。由于rooms()函数位于url调度器/rooms中,因此我不再需要添加“调度器”“在url上。我所做的是var pathname=window.location.pathname.split('/')[3];如果(路径名){readUrl='read';}太棒了!很高兴你解决了
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);
}
});
}