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

时间:2012-06-23 16:56:33

标签: javascript jquery json

我正在尝试使用Jquery UI Autocomplete使用Thesaurus 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>

3 个答案:

答案 0 :(得分:2)

$.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密钥和格式参数,但如果它们是动态的,则网址看起来像:

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

为了使你的代码更清洁一点,你甚至可以去:

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

答案 1 :(得分:1)

将数据移入url:

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

答案 2 :(得分:1)

你可以使用

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

并删除data选项。