繁体   English   中英

修改Geonames的JSON Ajax请求

[英]Modifying JSON Ajax Request for Geonames

我有一个示例网站的代码,但我想知道是否有人可以告诉我如何修改下面的代码只返回美国的结果。

这是我到目前为止的代码:

  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#city" ).autocomplete({
      source: 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
              }
            }));
          }
        });
      },
      minLength: 2,
      select: function( event, ui ) {
        log( ui.item ?
          "Selected: " + ui.item.label :
          "Nothing selected, input was " + this.value);
      },
      open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
      },
      close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
      }
    });
  });

显然你可以这样请求JSON:

http://ws.geonames.org/searchJSON?name_startsWith=Chic&country=US

所以我的问题是,如何修改上面的代码,在请求中包含“&country = US”。 我尝试了一些东西,但它完全打破了请求。

将country参数放在数据变量上:

      data: {
        featureClass: "P",
        style: "full",
        maxRows: 12,
        name_startsWith: request.term,
        country: "US"
      },

很简单!

在这种情况下,数据变量用于构建Ajax请求的查询字符串。

暂无
暂无

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

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