(CodeIgniter)Ajax返回主布局而不是所需的视图

时间:2016-10-23 17:17:57

标签: ajax codeigniter

我对ajax返回结果感到头疼。每个人都可以告诉我我的代码有什么问题吗?

我有一个名为“home.php”的主要布局,此文件中的一些重要代码如下:

        <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
            <meta name="description" content="">
            <meta name="author" content="">
            <link rel="icon" href="<?=base_url();?>favicon.ico">
            <title>IT Ebooks - Knowledge Sharing<?php if(isset($title_page)) echo " | ".$title_page ?></title>
            <link href="<?=base_url();?>css/bootstrap.min.css" rel="stylesheet">
            <link href="<?=base_url();?>css/font-awesome.min.css" rel="stylesheet">
            <link href="<?=base_url();?>css/custom.css" rel="stylesheet">
          </head>

          <body>

        <div id="main-content" class="col-md-9"><!--I will load the content here based on my controller & action-->
                <?php
                  if($this->router->fetch_class()=="ListBy"){
                    $this->load->view("listby");
                  }
                  elseif($this->router->fetch_class()=="Home"){
                    $this->load->view("index");
                  }
                  elseif($this->router->fetch_class()=="User"){
                    if($this->router->fetch_method()=="Signup")
                      $this->load->view('signup.php');
                  }
                ?>
        </div>

            <script src="<?=base_url();?>js/jquery-3.1.1.min.js"></script>
            <script src="<?=base_url();?>js/bootstrap.min.js"></script>
            <script src="<?=base_url();?>js/bootbox.js"></script>
            <script src="<?=base_url();?>js/listby.js"></script><!--this file do ajax function-->

        </body>
        </html>

我的家庭控制器:

        <?php
        class Home extends CI_Controller {

        public function __construct(){
        parent::__construct();
        $this->load->view('home.php');
        }

        public function index(){
        //Do nothing
        }

        } //class
        ?>

我的index.php文件:

<section id="latest"><!--Latest upload-->
  <div class="row">
  <div class="col-md-8">
  <p class="text-center text-primary header">Latest Uploads</p>
  </div>
    <div class="col-md-4">
    <div class="form-inline">
    <label>Books per page : </label>
    <select class="form-control" id="PageSize" name="PageSize">
    <option value="12" selected>12</option>
    <option value="24">24</option>
    <option value="36">36</option>
    </select>
    </div>
    </div>
    <div class="col-md-12" id="ajax-content">Ajax content loaded</div>
  </div>
</section>

我的ajax函数文件“listby.js”:

    $(document).ready(function(){
        $.ajax({
                type:"POST",
                url:"AjaxProcessing1/loadByCategory",
                data:"PageNo="+1+"&PageSize="+12,
                dataType: "text",
                success: function(response){
                    alert(response);//I used alert to show the problem
                    //$('#ajax-content').html(response);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }     
            });
    })

我的AjaxProcessing1控制器文件:

    <?php
    class AjaxProcessing1 extends CI_Controller {

    public function loadByCategory(){
    $PageNo = $this->input->post('PageNo');
    $PageSize = $this->input->post('PageSize');
    $this->load->model('model_book');
    $data['books'] = $this->model_book->get_latest_book($PageSize,$PageNo);
    $this->load->view('ajax_result1',$data);
    }

    } //class
    ?>

我的ajax_result1页面:

    <?php
    if(isset($books)){
    ?>
    <div class="row">
    <?php foreach($books as $book): ?>
    <div class="col-md-3 col-sm-6">
    <a href="<?=base_url();?>/ListBy/detail/<?=$book->id?>/<?=$book->slug?>">
    <img class="anhsach" src="<?=base_url();?>img/cover/<?=$book->image;?>" alt="<?=$book->slug;?>"/>
    </a>
    </div>
    <?php endforeach; ?>
    </div>
    <?php
    }
    ?>

So, until here. Everything's OK when I'm in home page, the ajax function return the ajax_result1 as expected


[Pic1 - Load as expected][1]



But, when I'm in ListBy controller, the return of the ajax function is the main layout?

我的ListBy控制器:

    <?php
    class ListBy extends CI_Controller {

    public function __construct(){
    parent::__construct();
    $this->load->model('model_book');
    }

    public function index(){
    //Nothing to do here
    }

    public function ListByCategory($category_id){
    $data['title_page'] = "List by category";
    $data['num_books'] = $this->model_book->number_of_books_bycategory($category_id);
    $data['category_id'] = $category_id;
    $this->load->view('home.php',$data);
    }

    } //class
    ?>

我的listby.php文件:

    <?php
    echo "<h3 class='text-center text-success'>".$num_books." book(s) found</h3>"
    ?>
    <section id="cate">
      <div class="row">
      <div class="col-md-8">
      <p class="text-center text-primary header">Select your books view per page</p>
      </div>
        <div class="col-md-4">
        <div class="form-inline">
        <label>Books per page : </label>
        <select class="form-control" id="PageSize" name="PageSize">
        <option value="12" selected>12</option>
        <option value="24">24</option>
        <option value="36">36</option>
        </select>
        </div>
        </div>
        <div class="col-md-12" id="ajax-content">Ajax content loaded</div>
      </div>
    </section>
    <input type="hidden" id="total_record" value="<?=$num_books?>"/>
    <input type="text" id="category_id" value="<?=$category_id?>"/>
    <div class="row text-center">
        <ul class="pagination">

        </ul>
    </div>

And the result I got when I'm in this controller (return the main layout):



[Not as expected][2]




So, what's going on with my code. Thanks so much for any help or suggestion in advanced.


  [1]: https://i.stack.imgur.com/PeADW.png
  [2]: https://i.stack.imgur.com/PAiO1.png

My project link in case you need: http://www.mediafire.com/file/ed1e31od1fk5vkg/itebooks-23Oct16.zip

1 个答案:

答案 0 :(得分:0)

Home扩展CI_Controller {public function __construct(){parent :: __ construct(); //$this->load->view('home.php'); //问题是:您无法在Home Controller构造函数中加载main / home布局}