我用geonames.org来自动完成城市和国家,但发现它是速度太慢是可靠的。我的代码如下,和不工作(等待大约10秒至看到自动完成结果)

这里

<强>旧(工作)代码: http://jsbin.com/umewo3/2 /编辑

  $(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
    });
  });

现在我使用YQL因为它们提供了非常快的响应。问题是,我似乎不知道如何正确映射的响应。你可以看到我送一个良好形式的请求,并得到响应回来 - 但我有点不与响应处理正确

这里

新(碎)代码: http://jsbin.com/aqoke3/2 /编辑

$(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
    });
  });
有帮助吗?

解决方案

我发现这个问题。我毕竟妥善处理它。我设法从JSONP的dataType除去P上。

所有很好: http://jsbin.com/aqoke3/4/edit

scroll top