2012-06-23 63 views
1

我試圖使用Jquery UI Autocomplete來檢索使用Thesaurus API的任何單詞的同義詞。jquery JSON從查詢字符串中刪除分隔符

我需要做下面的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> 

回答

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密鑰和格式參數,但如果他們是動態的,URL可能看起來像:

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

爲了使你的代碼乾淨了一點,你甚至可以去儘可能:

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

謝謝!有了這個修改,我將如何改變'response()'部分?它仍然需要嗎? – CyberJunkie

+1

@Cyber​​Junkie'success'回調和你的'response()'函數調用處理從api調用返回的數據。假設你想對api返回的數據做一些有用的事情,你絕對需要它們。我不知道你的響應函數是什麼樣子或需要做什麼,我不能比這更有幫助。 – Endophage

1

數據移動到網址:

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

你可以使用

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

並刪除data選項。

+0

其實這是行不通的。它會%編碼'/',所以最終的url看起來像這樣:'http://words.bighugelabs.com/api/2%2F %2F %2Fjson'然而,編碼單個分段是可能是一個好主意。 – Endophage

+0

@Endophage,謝謝先生,更新了答案。 –