微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax – 使用restful api进行简单搜索

我是一个完整的菜鸟尝试Ajax和 Jquery.通过在线教程,我成功地使用MysqL作为后端数据库创建了一个搜索引擎;

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

现在我想进一步搜索使用像Solr这样的RESTful api http:// localhost:9090 / solr / select?q = employee:(james blunt)& wt = json& indent = true
我需要有人请告诉我如何才能做到这一点.

解决方法

要创建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和做的事基于此.
对于Apache这样做,请阅读apache mod_rewrite:http://httpd.apache.org/docs/current/mod/mod_rewrite.html

有关RESTful API的更详细的处理,我可以建议本教程:http://www.restapitutorial.com/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐