[英]How to get the jSON data value?
I just want to ask about my problem. 我只想问一下我的问题。 I am a beginner in using jquery.
我是使用jquery的初学者。 My problem is I want to get the json string from my jquery ajax request but I don't know how.
我的问题是我想从我的jquery ajax请求中获取json字符串,但我不知道如何。 Because I want to check if the json string values are correct.
因为我想检查json字符串值是否正确。
Here's my code in jquery ajax: 这是我在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);
});
This is the code for the events. 这是事件的代码。
<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>
This is my code for accessing the controller (CodeIgniter) 这是我用于访问控制器的代码(CodeIgniter)
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);
}
What I want to get is: 我想要得到的是:
In my ajax. 用我的ajax There is a data that contains the value of keyword and data filter.
有一个包含关键字和数据过滤器值的数据。 Now What I need to do is get the data_filter value and pass it to the controller.
现在,我需要做的是获取data_filter值并将其传递给控制器。
You can get the response inside your ajax like this 您可以像这样在您的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
},
});
Ok got it just an error in my syntax. 好的,这只是我语法中的错误。 Thanks for the response guys.
感谢您的回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.