简体   繁体   English

如何通过jQuery显示JSON编码的数据?

[英]How to display json encoded data via jquery?

I already got a jquery autocomplete textbox which is displaying data correctly, but when I add $group in MongoDB query to do select distinct, I recieve the required json array as response but in the autocomplete text view nothing is shown, 我已经有了一个jQuery自动完成文本框,它可以正确显示数据,但是当我在MongoDB查询中添加$ group以选择“不重复”时,我收到了所需的json数组作为响应,但是在自动完成文本视图中未显示任何内容,

Below is my request result and the json array response from the processing page 以下是我的请求结果和处理页面中的json数组响应

My first query (Auto complete is populating perfectly) 我的第一个查询(自动填充功能非常好)

$result = $collection->aggregate(array(
array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"

 )
),
 array(
'$unwind' => '$Quote_Details'
 ),
        array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"


  )
   ),


       array(
    '$group' => array(
        '_id' => array(
            'value1' =>'$Quote_Details.Quote_Catagory'

        )
        ) 

    ),   





  array(
  '$project' => array(

 'value' => '$Quote_Details.Quote_Catagory'
   )      
   ), 



  ));

json result json结果

[{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Inspirational"},{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Inspirational"},{"_id":{"$id":"538443bf05a7d1226d000000"},"value":"Imagination"}]

New query(Autocomplete is populating but nothing is displaying) 新查询(正在填充自动完成功能,但未显示任何内容)

$result = $collection->aggregate(array(
 array(
'$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"

)
),
 array(
'$unwind' => '$Quote_Details'
 ),
        array(
   '$match' => array(

  'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
    'Name_Status' => "P",
    'Quote_Details.Quote_Status' => "p"


   )
 ),




 array(
    '$group' => array(
        '_id' => array(
            'value1' =>'$Quote_Details.Quote_Catagory'

        )
        )
       ), 

      ));

result json 结果json

[{"_id":{"value1":"Imagination"}},{"_id":{"value1":"Inspirational"}}]

My Jquery 我的jQuery

$(document).ready(function(){

                $("#catagr_id").autocomplete({
                    source:'insertionauto.php',
                    minLength:1,
                    select: function (event, ui) 
                    {
                         //label = ui.item.label;
                        var value1 = ui.item.value;
                        $("#authnames").val(value);
                        console.log("hi");

                        //alert(label);
                        //alert(value);

                        $("#quote_id").autocomplete({
                    source:'autocatagquote.php?postcode='+value1,
                    minLength:1,
                    select: function (event, ui) 
                    {
                        var label1 = ui.item.label;
                        var value1= ui.item.value;

                        console.log("hi");

                        //alert(label);
                        //alert(value);

                    }


                });

                    }


                });



              });

if my guess is correct I am getting a multi array as a second result and result is not displaying in autocomplete, but why ? 如果我的猜测是正确的,我将得到一个多数组作为第二个结果,并且结果未显示在自动完成中,但是为什么呢? Please help, there must be something about grouping 请帮助,关于分组一定有一些

So really about working with the "jqueryui" auto-complete method that expects a plain array of strings as a data-source. 因此,实际上是使用“ jqueryui”自动完成方法,该方法期望将纯字符串数组作为数据源。 But let's clean up your aggregation statement first as there are some un-necessary things in there that are not helping: 但是,让我们先清理聚合语句,因为其中有一些不必要的东西无济于事:

$result = $collection->aggregate(array(
    array(
       '$match' => array(
           'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
           'Name_Status' => "P",
           'Quote_Details.Quote_Status' => "p"
       )
    ),
    array(
        '$unwind' => '$Quote_Details'
    ),
    array(
        '$match' => array(
            'Quote_Details.Quote_Catagory' => new MongoRegex("/^$q/i"),
            'Name_Status' => "P",
            'Quote_Details.Quote_Status' => "p"
        )
    ),
    array(
        '$group' => array(
            '_id' => '$Quote_Details.Quote_Catagory'
        )
    ),
));

So there is no reason to nest the _id value in the end "group" result. 因此,没有理由将_id值嵌套在最后的“组”结果中。 And you actually want to strip down the objects as plain strings for your returned result. 而且,您实际上想将对象作为纯字符串剥离以得到返回的结果。

Then you are just looping through the php array: 然后,您只是遍历php数组:

  $myresult = array();

  for ( $i = 0; $i < sizeof( $result ); $i++ ) {
      $myresult[$i] = $result["result"][$i]["_id"];
  }

  json_encode( $myresult );

So then you just have an array of strings as required by the plugin method: 因此,您只需要按照plugin方法的要求设置一个字符串数组即可:

["Imagination","Inspirational"]

The point is that the plugin method included in the jqueryui API expects the results just to be an array of strings. 关键是jqueryui API中包含的plugin方法期望结果只是一个字符串数组。 You are returning objects with unexpected keys unless you manipulate it first. 您将使用意外的键返回对象,除非您先对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM