2013-07-01 43 views
0

這是我當前的代碼:http://jsfiddle.net/spadez/nHgMX/2/阿爾特阿賈克斯的Json查詢參數

$(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

要素類(ES)(默認爲所有要素類);可以 發生此參數超過一次,例如:要素類= P &要素類= A

我如何調整我的代碼要素類設置爲A和P不只是數p作爲它目前是?

+0

爲什麼這不工作'featureClass:「A,P」,'? – YD1m

+2

'featureClass:['A','P']'和'jQuery.ajaxSettings.traditional = true;'。請參閱[如何將相同類型的多個參數傳遞給jQuery獲取](http://stackoverflow.com/questions/12876373/how-do-you-pass-multiple-parameters-of-the-same-type-to -jquery-get)瞭解更多詳情。 –

+0

@ YD1m:因爲那麼數據將作爲'featureClass = A,B'傳遞,這似乎不被服務支持。 –

回答

1

我發現2個解決方案:

第一個是由@George康明斯建議使用方法:

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