0

我正在开发一个返回 JSON 的 API,但 API 调用有两个参数,“term”和“country”:

http://127.0.0.1:8000/internalapi/cidades/?country=PT&term=Barreir

如何修改此调用以支持额外的 API 参数“国家”?

<script>
$(function() {
    function log( message ) {
        $( "<div>" ).text( message ).prependTo( "#log" );
        $( "#log" ).scrollTop( 0 );
    }

    $( "#city" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://127.0.0.1:8000/internalapi/cidades/",
                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" );
        }
    });
});
</script>

有什么线索吗?

最好的祝福,


更新1:

选择国家的 HTML:

<div class="fieldWrapper">
    <label for="id_country">País:</label>
    <select id="id_country" name="country">
        <option selected="selected" value="">(Nothing)</option>
        <option value="PT">Portugal</option>
        <option value="ES">Espanha</option>
    </select>
</div>
4

1 回答 1

1

您应该将参数添加到数据对象中:

            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term,
                country: $('#id_country').find(":selected").val()
            },
于 2012-12-19T15:46:52.813 回答