我只是想問一下我的問題。我是使用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值並將其傳遞給控制器。
你可以發佈你正在獲取的示例reposnse json? –
好的謝謝你的迴應。我會查一下。 – Jerielle
下面是示例。 – Jerielle