我正在尝试在codeigniter中开发简单的CMS ..在主视图中有几个链接到其他页面,但我希望所有页面在指定的DIV内的相同视图中打开而不加载每个页面(AJAX LOADING)。我尝试使用AJAX调用来实现这一点,但是很难弄清楚为什么它不适合我。每当我点击链接时,它只是从控制器的索引方法加载视图,即LOGIN_VIEW。 这是我的cms视图,其中包含链接
<script type="text/javascript">
function onLinkClick(value){
alert('<?php echo base_url() ?>admin/load_view/'+value);
$.ajax({
url: '<?php echo base_url() ?>admin/load_view/',
type: 'POST',
data: {'view_param': value},
success: function(response)
{
//$('#mang_server').html(response);
alert("response recieved");
}
});
alert('after response');
}
</script>
<body>
<div id="main">
<div id="header"> <a href="" class="logo"><img src="../img/logo.gif" width="101" height="29" alt="" /></a>
<div id="middle">
<div id="left-column">
<h3>Header</h3>
<ul class="nav">
<?php foreach($ops as $operations)
{$op_name = $operations['admin_op'];
$op_link = $operations['link_to'];
?>
<li><a href="" id="" onclick="onLinkClick('<?php echo $op_link ;?>')"><?php echo $op_name ;?></a></li>
<?php } ?>
</ul>
<div id="mang_server"> </div>
这两个警报都在工作......但是ajax电话正在搞乱。
这是我的控制器方法
public function index()
{
if($this->session->userdata('admin_name'!='')){
$this->data['title'] = "Admin Area";
$this->data['ops'] = $this->admin_model->get_admin_ops();
$this->load->view('admin_view.php',$data);
} else {
$data['title'] = "Admin login";
$this->load->view('admin_login.php',$data);
}
}
public function load_view($view_name)
{
$data['view_link'] = $this->admin_model->load_view_model($view_name);
$this->load->view($data);
}
请告诉我我犯的错误。
答案 0 :(得分:0)
您正在以错误的方式执行此操作,请考虑以下代码以通过ajax加载视图
Javascript部分:
$.ajax({
url: '<?php echo base_url() ?>admin/load_view/',
type: 'POST',
dataType:'json',
data: {'view_param': value},
success: function(response)
{
$('#mang_server').html(response.html_view);
}
});
管理员控制器:
public function load_view($view_name){
$data['view_link'] = $this->admin_model->load_view_model($view_name);
// IF $data['view_link'] is the name of the view file you are trying to load
$response['html_view']$this->load->view($data['view_link'],'',TRUE);
// While loading a view in codeigniter it accepts three parameters
// $this->load->view("name of the view file","data to be passed to the view or not","Returning views as data");
// For more refer at the end of the page http://ellislab.com/codeigniter/user-guide/general/views.html
echo json_encode($response);
}