2013-10-31 46 views
0

我只是想问一下我的问题。我是使用jQuery的初学者。 我的问题是我想从我的jquery ajax请求获取json字符串,但我不知道如何。因为我想检查json字符串值是否正确。如何获取jSON数据值?

这里是我的jQuery中的Ajax代码:

$('#search-btn').on('click',function(){ 

      var query = $("#keyword").val(); 

      var image = "<?php echo base_url()."/resources/loading/loading43.gif"; ?>"; 

      $('#loading').html("<img src='"+image+"' class='loeader' align='center' />"); 

      var query_url = "<?php echo site_url('item_controller/searchItem'); ?>"; 

      $.ajax({ 

       type:'POST', 
       url: query_url, 
       data:{query: $("#keyword").val(), data_filter: $("#keyword").attr("data-selection")}, //how can I get the value of data_filter? and pass it to the controller? 
       dataType:'json', 
       async: false, 
       success:function(d){ 
         //some codes for success. . . 

      }, 
}); 



$("#selection_filter").on('change',function(){ 

    var filter = $("#selection_filter").val(); 

    $("#keyword").attr("data-selection",filter); 
}); 

这是事件的代码。

<table border="1" style="width: 100%; align: left" cellpadding="10" cellspacing="10" align="left"> 
      <tr> 
       <td colspan="3"> 
        <h5>SEARCH ITEM</h5> 
       </td> 
      </tr> 
      <tr> 
       <td style="width: 15%"> 
        <label>Choose search filter: </label> 
       </td> 
       <td style="width: 25%"> 
        <select id="selection_filter"> 
         <option value="code">ITEM CODE</option> 
         <option value="itemname">ITEM NAME</option> 
        </select> 
       </td> 
       <td> 
        <input type="text" name="keyword" id="keyword" style="width: 80%" data-selection="code" /> <input type="button" value="SEARCH" id="search-btn" class="k-button" style="font-size: 12px" /> 
       </td> 
      </tr> 
     </table> 

这是我访问控制器(笨)

public function searchItem(){ 

      $query = $this->input->post('query'); 
      $filter = $this->input->post('data_filter'); 

      if($filter == "code"){ 
       $querySearch = "SELECT item_code,item_name from items WHERE item_code LIKE '%".$query."%' GROUP BY item_code"; 
      }else{ 
       $querySearch = "SELECT item_code,item_name from items WHERE item_name LIKE '%".$query."%' GROUP BY item_code"; 
      } 

      $resultSearch = $this->db->query($querySearch); 
      $count = $resultSearch->num_rows(); 

      echo json_encode($resultSearch->result_array()); 
      //echo json_encode($querySearch); 

     } 

我想要得到的是代码:

在我的AJAX。有一个数据包含关键字和数据过滤器的值。现在我需要做的是获取data_filter值并将其传递给控制器​​。

+0

你可以发布你正在获取的示例reposnse json? –

+0

好的谢谢你的回应。我会查一下。 – Jerielle

+0

下面是示例。 – Jerielle

回答

1

你可以得到这样你的Ajax内响应

$.ajax({ 
      type:'POST', 
      url: query_url, 
      data:{query: $("#keyword").val(), data_filter: $("#keyword").attr("data-selection")}, 
      dataType:'json', 
      async: false, 
      success:function(res){ 
       console.log(res.item_code);//Will give you the item code 
       console.log(res.item_name);//Will give you the item name 
     }, 
}); 
+0

好吧,先生,但我想要的是来自ajax数据的data_filter值。我需要将ajax数据传递给控制器​​。 – Jerielle

0

确定了它在我的语法只是一个错误。谢谢你们的回应。