2012-06-23 61 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,谢谢先生,更新了答案。 –