2016-10-23 13 views
1

私はajaxの返信結果で頭痛があります。誰も私のコードに何が間違っているかを見せてもらえますか?(CodeIgniter)Ajaxは、必要なビューの代わりにメインレイアウトを返します。

 <!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ファイル:

は、私が "home.php" という名前のメインレイアウト、belowsのようにこのファイルのいくつかの重要なコードを持っています
<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 

答えて

0

ホーム拡張CI_Controller {public function __construct(){parent :: __ construct(); //$this->load->view('home.php '); //問題はHomeコントローラの構築機能でメイン/ホームレイアウトを読み込めません}

関連する問題