我正在尝试使用Jquery UI Autocomplete来使用同义词 API检索任何单词的同义词。
我需要发出以下 json GET 请求来访问 API
http://words.bighugelabs.com/api/{version}/{api key}/{word}/{format}
然而,Jquery 生成以下返回404 Not Found
http://words.bighugelabs.com/api/?v=2&key=mykey&word=some-word&format=json
是否可以轻松去除分隔符?
脚本
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#thesaurus" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://words.bighugelabs.com/api/",
dataType: "json",
data: {
v: "2",
key: "mykey", //actually numbers
word: request.term,
format: "json"
//maxRows: 12,
//name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.noun ? ", " + item.noun : "") + ", " + item.syn,
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" );
}
});
});
HTML
<input id="thesaurus" />
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>