JQuery自动完成输出问题

时间:2016-02-08 21:18:26

标签: jquery json

我已经看过很多次这个问题了,对于我的生活,我似乎无法弄明白这一点。我有一个远程源我用来获取JSON,我能够看到JSON在控制台中返回,我只是无法输出它。我需要的是输出到自动完成的“描述:”和“状态”。有人可以告诉我哪里出错了吗?

这是我的功能:

$(function() {
        $( "#locationSearch" ).autocomplete({
            source: function( request, response ) {
            $.ajax({
                type: "GET",
                url: "https://www.datasample.com/Services/AutoComplete.ashx",
                dataType: "jsonp",
                data: {
                  term: request.term,
                },
                success: function( data ) {
                    response( $.map( data.results, function( result ) {
                        return {
                            label: result.Description,
                            value: result.State
                        }
                    }));
                }
            });
            },
            minLength: 3
        });
    });

这是返回的JSON:

  

{“term”:“Boston”,“results”:[{“Description”:“Boston,MA”,“Lat”:0.0,“Lng”:0.0,“Category”:“City”,“Radius” “:null,”State“:”MA“},{”Description“:”Boston,NY“,”Lat“:0.0,”Lng“:0.0,”Category“:”City“,”Radius“:null, “State”:“NY”},{“Description”:“Boston Heights,OH”,“Lat”:0.0,“Lng”:0.0,“Category”:“City”,“Radius”:null,“State” : “OH”}]}

这是HTML:

<input name="locationSearch" id="locationSearch" type="text" />

1 个答案:

答案 0 :(得分:0)

这是一个例子

data = {"term":"Boston","results":[{"Description":"Boston, MA","Lat":0.0,"Lng":0.0,"Category":"City","Radius":null,"State":"MA"},{"Description":"Boston, NY","Lat":0.0,"Lng":0.0,"Category":"City","Radius":null,"State":"NY"},{"Description":"Boston Heights, OH","Lat":0.0,"Lng":0.0,"Category":"City","Radius":null,"State":"OH"}]};

$.map(data.results, function(result){
   return {
        label: result.Description,
        value: result.State
   }
});

输出

"[{"label":"Boston, MA","value":"MA"},{"label":"Boston, NY","value":"NY"},{"label":"Boston Heights, OH","value":"OH"}]"