为什么SOLR不能与jQuery一起使用?

时间:2013-07-02 15:37:12

标签: javascript jquery solr

我使用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;
});

我没有得到“成功”警报。永远不会执行回调函数。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

检查Documentation

请务必将json.wrf=?参数添加到请求网址,以便调用成功方法。

  

json.wrf = function - 围绕JSON响应添加包装函数,   在AJAX中有用,带有用于指定JavaScript的动态脚本标记   回调函数。

您仍然可以直接使用getJson函数从Solr获取json响应。

答案 1 :(得分:1)

我认为您提供的网址中存在小错误。

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

希望它可能有用。

相关问题