2014-03-27 124 views
0

我需要通過java腳本獲取solr查詢響應。我做了以下代碼來獲得響應。但是響應文本只顯示空字符串。它不會從solr中檢索數據。請指導我我犯的錯誤。謝謝..通過javascript獲取Solr響應爲json

function getSolrResponse() { 
    var strURL = "http://localhost:8983/solr/Core1/select"; 
    var xmlHttpReq = false; 
    if (window.XMLHttpRequest) { 
     xmlHttpReq = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlHttpReq.open('POST', strURL, true); 
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    xmlHttpReq.onreadystatechange = function() { 
     if (xmlHttpReq.readyState == 4) { 
      alert(xmlHttpReq.responseText); 
     } 
    }; 
    xmlHttpReq.send("q=*:*&wt=json"); 
} 
+0

的代碼似乎是正確的。你可以嘗試直接查詢這個curl -d「q = *:*&wt = json」http:// localhost:8983/solr/Core1/select來查看會發生什麼。您的問題可能出現在solr服務器配置中。試着用http:// localhost:8983/solr –

回答

0

看看Solr自己的日誌。它會向你顯示你得到的查詢字符串和錯誤信息。

特別是在這裏,你爲什麼要做POST?從GET開始(更易於調試)然後進行調整。

0

我認爲這個問題是在代碼的最後一行:

xmlHttpReq.send("q=*:*&wt=json"); 

實際上,Solr的好好嘗試一下收到該請求該namve /值對,它只是認爲你的要求沒有查詢部分(wt = json部分是可選的)。如果您將其附加到請求URL,它將返回適當的JSON。這是你的代碼的一個版本,這個修改,我已經測試和工程確定:

function getSolrResponse() { 
     var strURL = "http://localhost:8983/solr/DOWNMUSIC/select?q=*:*&wt=json"; 
     var xmlHttpReq = false; 
     if (window.XMLHttpRequest) { 
      xmlHttpReq = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { 
      xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlHttpReq.open('POST', strURL, true); 
     xmlHttpReq.setRequestHeader('Content-Type', 'application/json'); 
     xmlHttpReq.onreadystatechange = function() { 
      if (xmlHttpReq.readyState == 4) { 
       alert(xmlHttpReq.responseText); 
      } else { 
       //alert("ELSE: "+xmlHttpReq.responseText); 
      } 
     }; 
     xmlHttpReq.send(); 
    } 
+0

上的管理界面執行查詢我認爲'application/json'作爲請求內容類型是不正確的。它的工作原理僅僅是因爲你沒有發送POST數據,但查詢在URL中。 – Zac