2013-06-21 35 views
0

我有我从一个示例站点后的代码,但我想知道是否有人能告诉我如何修改下面的代码,只返回来自美国的结果。修改Geonames的JSON Ajax请求

这是我的代码至今:

$(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“。我已经尝试了一些东西,但它完全打破了这个要求。

回答

2

放在数据变量全国参数:

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

简单的那样!

本例中的数据变量用于构建Ajax请求的查询字符串。