簡體   English   中英

為什么SOLR不適用於jQuery?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM