jquery JSON 从查询字符串中删除分隔符

jquery JSON remove separators from query strings

本文关键字:删除 分隔符 字符串 查询 JSON jquery      更新时间:2023-09-26

我正在尝试使用Jquery UI自动完成功能来使用同义词库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>

数据参数对$.ajax()函数的全部意义在于创建一个查询字符串(POST 和 GET 都使用查询字符串,它们只是作为请求有效负载的不同部分发送)。您只想使用简单的字符串串联来构建您的 URL。

$.ajax({
    url: "http://words.bighugelabs.com/api/2/mykey/" + request.term + "/json",
    dataType: "json",
    success: function( data ) {
        response( $.map( data.geonames, function( item ) {
            return {
                label: item.name + (item.noun ? ", " + item.noun : "") + ", " + item.syn,
                value: item.name
            }
        }));
    }
});

您有静态版本、api 密钥和格式参数,但如果它们是动态的,则 url 将如下所示:

"http://words.bighugelabs.com/api/" + version + "/" + api_key + "/" + request.term + "/" + format

为了使您的代码更简洁一些,您甚至可以执行以下操作:

"http://words.bighugelabs.com/api/" + [version,api_key,request.term,format].join("/")

将数据移动到 url 中:

    $( "#thesaurus" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://words.bighugelabs.com/api/2/" + "mykey/" + request.term + "/json",
                ....
            });
        },
你可以

只使用

url:"http://words.bighugelabs.com/api/"
           +"2/"+encodeURIComponent(mykey)+"/"
           +encodeURIComponent(request.term)+"/json"),

并删除data选项。