2013-05-12 52 views
4

我是一个完整的noob尝试我的手在Ajax和Jquery上。通过遵循在线教程,我成功地创建了一个使用MySQL作为后端数据库的搜索引擎;使用restful api简单搜索

<script> 
$(function() { 
$(".search_butn").click(function() { 
    // getting the value that user typed 
    var searchString = $("#input_box").val(); 
    // forming the queryString 
    var data   = 'search='+ searchString; 
    // if searchString is not empty 
    if(searchString) { 
     // ajax call 
     $.ajax({ 
      type: "POST", 
      url: "search.php",  //server-side script to db (mysql) 
      data: data, 
      beforeSend: function(html) { // this happens before actual call 
       $("#results").html(''); 
       $("#searchresults").show(); 
       $(".word").html(searchString); 
      }, 
      success: function(html){ // this happens after we get results 
       $("#results").show(); 
       $("#results").append(html); 
      } 
     }); 
    } 
    return false; 
}); 
}); 
</script> 

<form method="post" action="search.php"> 
<div id="DIV"> 
    <input type="text" name="search" id="input_box" class='input_box'/> 
    <input type="submit" value="Search" class="search_butn" /> 
</div> 
</form><br/> 

<div> 
<div id="searchresults"> </div> 
<ul id="results" class="update"> 
</ul> 
</div> 

现在我想更进一步,通过使用一个RESTful API,这样一个从Solr的http://localhost:9090/solr/select?q=employee%3A%28james+blunt%29&wt=json&indent=true 我需要有人来请告诉我,我怎么能去这样做搜索。

回答

1

要创建一个RESTful API,您可以编写一些PHP代码来截断请求的URL。 你应该让Apache - 你的网络服务器,我想 - 将所有具有特定前缀的URL重定向到这个PHP脚本。

因此,假设用户请求http://www.somename.com/my_api/some/shiny?suffix,您希望Apache将此URL重定向到my_api.php脚本,这样my_api.php可以砍掉整个URL并根据该URL执行相关操作。 对于Apache这样做,在Apache mod_rewrite的读了起来:http://httpd.apache.org/docs/current/mod/mod_rewrite.html

对于RESTful API中的更详细的治疗,我可以建议本教程:http://www.restapitutorial.com/