[英]Why does SOLR not work with jQuery?
我使用jQuery的$.ajax()
将请求发送到SOLR服务器,并以JSON格式获取结果,但没有用。 我的代码是正确的,因此我无法弄清楚出了什么问题。 如果将URL粘贴到浏览器的地址栏中,则可以正确获取JSON输出。
我用Google搜索它,并在Stack Overflow上找到了一些类似的问题:一个问题说是改用$.getJSON
,没有给出任何理由,但这对我也不起作用。
现在,我知道有一个AJAX-SOLR框架,并且我将使用它,但是我想知道为什么SOLR和jQuery存在问题。
以下是我尝试使用$.getJSON
尝试,以防万一这是我的错误。
$('#searchForm').on('submit', function(){
// get the user entered search query
var query = $('#queryBox').val();
// send the AJAX request to solr and get the results
$.getJSON('http://localhost:8983/solr/select/?q='+query+'&wt=json&indent=true', function(searchResult){
alert('success');
});
alert(query);
return false;
});
我没有收到“成功”警报。 回调函数永远不会执行。 我怎样才能解决这个问题?
检查文件
确保添加json.wrf=?
参数作为请求URL的参数,因此调用成功方法。
json.wrf = function-在JSON响应周围添加包装函数,在带有动态脚本标签的AJAX中非常有用,用于指定JavaScript回调函数。
您仍然可以直接使用getJson函数从Solr获取json响应。
我认为您输入的网址存在一些小错误。
$.getJSON('http://localhost:8983/solr/select/?q='+query+'&wt=json&indent=true', function(searchResult){
$.getJSON('http://localhost:8983/solr/select?q='+query+'&wt=json&indent=true', function(searchResult){
我只是在选择后删除了“ /”。 使用下面的AJAX代码,我可以正常工作。
<html>
<head>
<script type="text/javascript">
function callAjax() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
alert(xmlhttp);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var searchStr = document.getElementById("searchQuery");
xmlhttp.open("GET","http://localhost:8080/solr/select?q="+(searchStr.value),true);
xmlhttp.send();
}
</script>
<title>ajaxTest</title>
</head>
<body>
<form>
<input type="text" id="searchQuery">
<input type="button" value="SUBMIT" onclick="callAjax()">
<div id="myDiv"></div>
</form>
</body>
</html>
希望它可能有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.