Domanda

Stavo usando geonames.org il completamento automatico di città e lo stato, ma l'ho trovato troppo lento per essere affidabile.Il mio codice è il seguente, e funziona (attendere circa 10 secondi per vedere i risultati del completamento automatico)

Vecchio (di lavoro) codice qui: http://jsbin.com/umewo3/2/edit

  $(function() {
    $( "#sf_city" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "http://ws.geonames.org/searchJSON",
          dataType: "jsonp",
          data: {
            featureClass: "P",
            style: "full",
            maxRows: 10,
            country: 'US',
            name_startsWith: request.term
          },
          success: function( data ) {
            response( $.map( data.geonames, function( item ) {
              return {
                value: item.name + (item.adminName1 ? ", " + item.adminCode1 : "")
              }
            }));
          }
        });
      },
      minLength: 2
    });
  });

Ora sto usando YQL in quanto forniscono una risposta molto più rapida.Il problema è che io non riesco a capire come associare correttamente la risposta.Potete vedere sono l'invio di un ben fatta la richiesta, e ottenere la risposta - ma io sono, in qualche modo, non a che fare con la risposta corretta.

Nuovo (rotto) codice qui: http://jsbin.com/aqoke3/2/edit

$(function() {
    $( "#sf_city" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "http://query.yahooapis.com/v1/public/yql",
          dataType: "json",
          data: {
            q: 'select name,admin1.code from geo.places where text="' + request.term + '*" and country.code="US" limit 10 | sort(field="popRank", descending="true")',
            format: 'json',
            callback: 'cbfunc'
          },
          success: function( data ) {
            response( $.map( data.query.results.place, function( item ) {
              return {
                value: item.name
              }
            }));
          }
        });
      },
      minLength: 2
    });
  });
È stato utile?

Soluzione

Ho trovato il problema.Ho a che fare con esso correttamente dopo tutto.Sono riuscito a rimuovere il P dal jsonp tipo di dati.

Tutto bene: http://jsbin.com/aqoke3/4/edit

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top