gpt4 book ai didi

ajax - 使用restful api的简单搜索

转载 作者:行者123 更新时间:2023-12-01 05:21:21 25 4
gpt4 key购买 nike

我是一个完整的菜鸟,正在尝试使用 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%3A%28james+blunt%29&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/

关于ajax - 使用restful api的简单搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com