【问题标题】:How to get the jSON data value?如何获取 JSON 数据值?
【发布时间】:2013-10-31 06:24:24
【问题描述】:

我只是想问一下我的问题。我是使用 jquery 的初学者。 我的问题是我想从我的 jquery ajax 请求中获取 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>

这是我访问控制器的代码(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);

        }

我想得到的是:

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

【问题讨论】:

  • 你能发布一个你得到的示例reposnse json吗?
  • 好的,感谢您的回复。我会检查的。
  • {"item_code":"162103","item_name":"Sun00-INCOGNITO\r"}
  • 解题后无需修改标题。我更新了标题。

标签: javascript jquery ajax json codeigniter


【解决方案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
         },
});

【讨论】:

  • 好的,先生,但我想要的是来自 ajax 数据的 data_filter 值。我需要将 ajax 数据传递给控制器​​。
【解决方案2】:

好的,这只是我的语法错误。谢谢大家的回复。

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 2021-03-25
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多