简体   繁体   English

AJAX POST无法在CODEIGNITER中加载div视图

[英]AJAX POST not working to load div view in CODEIGNITER

Help me out with my ajax thing...Alright what i am trying to do is i have a side column navigation panel in may main view..all the navigation links is fetched from database.now what i want is when i click any link it should load the CI view div in the same main view but in the middle panel and if click second link it should replace the old view div with new one without loading page.The Divs to be loaded after click are placed in the views folder of the project(CI views folder). 帮我解决我的Ajax问题...好吧,我想做的是,我可能在主视图中有一个侧栏导航面板。.所有导航链接都是从database.now中获取的。当我单击任何链接时,我想要的是它应在相同的主视图中但在中间面板中加载CI视图div,如果单击第二个链接,则应将新的div替换为旧视图div,而不加载页面。单击后要加载的Divs放置在的views文件夹中项目(CI视图文件夹)。 Now what i have tried is when link is clicked i call the ajax with the view name passes as the data to the URL(/controller/method) and controller load the view and on ajax success the view should be loaded in the middle panel as the new div. 现在我尝试的是单击链接时,我将视图名称调用ajax,同时将数据传递到URL(/ controller / method),控制器加载该视图,而ajax成功时,该视图应以以下方式加载到中间面板中:新股

View Code: 查看代码:

script code: 脚本代码:

<script type="text/javascript">

        $(document).ready(function(){

        $("a.links").click(function(e){

        e.preventDefault();
        alert("alert on click");
        var platform_id = $(this).attr('id');
        alert(platform_id);


        $.ajax({

             url: '<?php echo base_url() ?>admin/load_view', 
             type: 'POST',
             datatype:'json',
             data: {'view_param': platform_id},
             success: function(response)
             {
                 if (response.success == 1)
                    {
                       alert('success');
                    }
                    else{
                    alert('unseccessful');   //just a alert to see if response is successful              
                    }       
             },
             error: function(response){
                    alert(response.message);
                }

        }); 


        })
        })

        </script>


<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="<?php echo $op_name ; ?>" class="links"><?php echo $op_name ;?></a></li>
            <?php } ?>
      </ul>
          <a href="http://all-free-download.com/free-website-templates/" class="link">Link here</a> <a href="http://all-free-download.com/free-website-templates/" class="link">Link here</a> 
          </div>

middle panel div: 中间面板div:

<div id="mang_server"> </div>

Controller code: 控制器代码:

public function load_view(){

$view_name = $this->input->post('view_param');
log_message('var dump', var_export($view_name));
$data['view_link'] = $this->admin_model->load_view_model($view_name);
$response['html_view'] = $this->load->view($data['view_link'],'',TRUE);
echo json_encode($response);
//$this->load->view($data);
}

I tried to debug it also i observe that it goes success portion of the ajax but it alerts the else part of it. 我尝试调试它,但同时我也观察到它在ajax中成功了,但是它提醒了它的其他部分。

error portion alert: 错误部分警报:

'Manage Servers'{"success":1,"message":"Some success message","html_view":"<table class=\"listing form\" cellpadding=\"0\" cellspacing=\"0\">\n          <tr>\n            <th class=\"full\" colspan=\"2\">Header Here<\/th>\n          <\/tr>\n          <tr>\n            <td class=\"first\" width=\"172\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr class=\"bg\">\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr>\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr class=\"bg\">\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n        <\/table>\n        <p>&nbsp;<\/p>"}

in your controller you didn't assigne $response['success'] and $response['message'] which you need in the front end. 在您的控制器中,您没有分配前端所需的$response['success']$response['message'] Change the controller code into, 控制器代码更改为

public function load_view()
{
    $view_name = $this->input->post('view_param');
    log_message('var dump', var_export($view_name, true));
    $data['view_link'] = $this->admin_model->load_view_model($view_name);

    $response['success'] = 1;
    $response['message'] = "Some success message";
    $response['html_view'] = $this->load->view($data['view_link'],'',TRUE);
    echo json_encode($response);
    exit;
}

Hope this helps :) 希望这可以帮助 :)

Updated: 更新:

NOTE: in log_message('var dump', var_export($view_name, true)); 注意:log_message('var dump', var_export($view_name, true)); , var_export() need a second parameter to return the value. var_export()需要第二个参数来返回值。 Otherwise, it will output a text and causes problem with your ajax response. 否则,它将输出文本并导致ajax响应出现问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM