更改Ajax Json查询参数

时间:2013-07-01 20:12:09

标签: javascript jquery ajax json

这是我目前的代码:http://jsfiddle.net/spadez/nHgMX/2/

  $(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: 7,
        name_startsWith: request.term,
        country: "UK"
      },           
          success: function( data ) {
            response( $.map( data.geonames, function( item ) {
              return {
                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                value: item.name
              }
            }));
          }
        });
      },
      minLength: 1,
      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" );
      }
    });
  });

此链接指出:http://www.geonames.org/export/geonames-search.html

  

featureclass(es)(默认=所有要素类);这个参数可能   不止一次出现,例如:featureClass = P&amp; featureClass = A

如何调整我的代码以将要素类设置为A和P而不仅仅是当前的P?

1 个答案:

答案 0 :(得分:1)

我找到了2个解决方案:

首先是@George Cummins建议的使用方法:

jQuery.ajaxSettings.traditional = true; 

...

$.ajax({
          url: "http://ws.geonames.org/searchJSON",
          dataType: "jsonp",
       data: {
        featureClass: ["A","P"],

第二,你可以在查询字符串中添加参数:

$.ajax({
          url: "http://ws.geonames.org/searchJSON?featureClass=A&featureClass=P", // you can concantenate it