2015-04-24 72 views
0

我想通過使用AJAX實現搜索模塊。AJAX JSON和路由在cakephp

有一個在我的資料控制器的index.ctp文件和我已鏈接的項目到我search.ctp文件的我的index.ctp文件,該文件是本下頁控制器如下:

<li><?= $this->Html->link(__('Search Products'),['controller'=>'Pages','action' => 'search']) ?></li> 

對於search.ctp頁面上顯示的網址是: http://onlineelectronic.com/pages/search

在我search.ctp文件中的代碼如下:

<head> 
    <title> Search Results</title> 
    <?php echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', array('inline' => false));?> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#Submit1").click(function() { 
       $.ajax({ 
        type: 'post', 
        url: '/Items/searchData", 
        data: { 
         name: "search" 
        }, 
        beforeSend: function(){ 
         $("#resultField").html("Loading..."); 
        }, 
        success: function(response) { 
         jQuery('#resultField').val(response); 
        }, 
        error: function(response, error) { 
         alert("Search :Error"+response.error()); 
        }, 
        dataType: 'json', 
        global: false 
       }); 
       return false; 
      }); 
     }); 
    </script> 
</head> 

<div> 
    <?= $this->Form->create() ?> 
    <fieldset> 
     <legend><?= __('Search Item') ?></legend> 
     <?php 
     echo $this->Form->input('search',['label'=>'Search']); 
     ?> 
    </fieldset> 

    <?= $this->Form->button('Search Items',['label'=>'Submit1']); ?> 
    <?= $this->Form->end() ?> 

</div> 

<div id="resultField"> 

</div> 

在我ItemsCont奧勒頁searchData方法的實現是這樣的:

class ItemsController extends AppController 
{ 


    public $helpers = ['Form', 'Html', 'Time']; 

    public function initialize() 
    { 
     parent::initialize(); 
     $this->loadComponent('RequestHandler'); 
    } 

    public function search(){ 
    //dummy 
    } 

    /** 
    *obtains search result for a given string. 
    */ 
    public function searchData() 
    { 
     $this->layout = 'ajax'; 
     echo "here"; 
     $search_data=[]; 
     var_dump($search_data); 
     //$search_results = []; 
     if ($this->request->is('post')) { 
      $search_data= $this->request->data; 
      $search_data=implode("|",$search_data); 
      $search_results = $this->Items->find('all', array('conditions'=>array('Items.itemName LIKE'=>"%$search_data%"))); 
      if(!empty($search_results)) { 
       $this->set(compact($search_results)); 
       $this->set('_serialize',array('search_results')); 
       return json_encode($search_results); 
      } 
     } 

    } 



    public function beforeFilter(Event $event) 
    { 
     parent::beforeFilter($event); 

     $this->Auth->allow(['index', 'view','search','searchData']); 


    } 
} 

我的問題是,SearchData方法不被調用,我沒有得到任何JavaScript錯誤also.How讓我的確認方法被調用上壓在按下按鈕後。是否由於json中的url?

回答

0

我看到2個可能的問題。首先,在上述ItemsController,你必須讓searchData方法

// change this line 
$this->Auth->allow(['index', 'view','search']); 
// to this 
$this->Auth->allow(['index', 'view','searchData']); 

也確保,你有正確的jQuery選擇

// try to change this line 
<?= $this->Form->button('Search Items',['label'=>'Submit1']); ?> 
// to this 
<?= $this->Form->button('Search Items',['id'=>'Submit1']); ?> 

編輯:使更多更正的javascript:

  1. ajax傳遞的數據應雙引號

    data: { 
        name: "search" 
    }, 
    
  2. 添加return false;點擊處理程序結束,所以可以防止submiting形式和刷新頁面

另外請注意,您必須在Template/Items/search_data.ctp

+0

我已經有模板searchData兩個變化,但它似乎沒有任何效果。 我現在已經包含在項目/模板下的search.ctp文件,從那裏我通過使用URL調用searchData方法:'/ Items/searchData'; 我將編輯我的帖子,以便它反映我當前的代碼 我觀察到的是,表單試圖從Items Controller調用操作方法search(),因此我在控制器中包含了一個虛擬方法,沒有code.But從ajax代碼I我無法訪問searchdata和beforeSend不工作@Roman – Veda

+0

我編輯了我的答案,我相信你的問題只在JavaScript。 AJAX應該調用'searchData'方法,'搜索'方法不是必需的 –

+0

如上所述,編輯上面的代碼,現在我得到一個警報,它在json中顯示錯誤。 我收到的消息是「Search:Error [object Object]。 爲什麼對象返回錯誤 – Veda