автозаполнение с помощью freebase и jsonp - PullRequest
3 голосов
/ 27 февраля 2012

Я пытаюсь использовать пример автозаполнения Jquery-ui для работы с Freebase.Дополнительно я использую плагин tag-it ...

Это то, что я пытаюсь, но не работает:

$(function() {
    $("#tags").tagit({
        tagSource: function( request, response ) {
                        $.ajax({
                            url: "https://www.googleapis.com/freebase/v1/search",
                            dataType: "jsonp",
                            data: {
                                limit: 12,
                                name: request.term
                            },
                            success: function( data ) {
                                response( $.map( data.result, function( item ) {
                                    return {
                                        label: item.name,
                                        value: item.name
                                    }
                                }));
                            }
                        });
        }
    });
});

Использование чего-то вроде: https://www.googleapis.com/freebase/v1/search?query=ambrose%20b&indent=true

Пример JSON

{
  "status": "200 OK",
  "result": [
    {
      "mid": "/m/0dkdnj6",
      "name": "Ambrose B. Rathborne",
      "notable": {
        "name": "Author",
        "id": "/book/author"
      },
      "lang": "en",
      "score": 71.059212
    },
    {
      "mid": "/m/0m17",
      "name": "Ambrose Bierce",
      "notable": {
        "name": "Journalist",
        "id": "/m/0d8qb"
      },
      "lang": "en",
      "score": 34.444187
    }.....

Из примера, который работает:

$(function() {
    $("#tags").tagit({
        tagSource: function( request, response ) {
                        $.ajax({
                            url: "http://ws.geonames.org/searchJSON",
                            dataType: "jsonp",
                            data: {
                                featureClass: "P",
                                style: "full",
                                maxRows: 12,
                                name_startsWith: request.term
                            },
                            success: function( data ) {
                                response( $.map( data.geonames, function( item ) {
                                    return {
                                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                        value: item.name
                                    }
                                }));
                            }
                        });
        }
    });
});

1 Ответ

2 голосов
/ 27 февраля 2012

Вы поняли это почти правильно. Вы ошиблись, попробуйте:

$(function() {
    $("#tags").tagit({
        tagSource: function( request, response ) {
                        $.ajax({
                            url: "https://www.googleapis.com/freebase/v1/search",
                            dataType: "jsonp",
                            data: {
                                limit: 12,
                                query: request.term
                            },
                            success: function( data ) {
                                response( $.map( data.result, function( item ) {
                                    return {
                                        label: item.name,
                                        value: item.name
                                    }
                                }));
                            }
                        });
        }
    });
});
...