gpt4 book ai didi

javascript - 修改 Geonames 的 JSON Ajax 请求

转载 作者:行者123 更新时间:2023-12-02 18:36:54 24 4
gpt4 key购买 nike

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

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

  $(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”。我已经尝试了一些方法,但它完全破坏了请求。

最佳答案

将国家/地区参数放在数据变量上:

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

就这么简单!

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

关于javascript - 修改 Geonames 的 JSON Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17236761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com