2012-05-10 105 views
1

我使用Solr's JSON output for AJAX example作爲我的項目的基礎。不過,我通過在表單中​​添加下拉菜單修改了一些內容,並將其添加到了參數中。Solr&AJAX設置響應查詢正確

PARAMS:

function getstandardargs() { 
    var params = [ 
     'wt=json' 
     ,'facet=true' 
     ,'facet.field=brand1' 
     ,'facet.field=brand2' 
     ,'facet.field=brand3' 
     ,'facet.field=brand4' 
     ,'facet.limit=2' 
     ]; 

下拉菜單:

<form name="f1" onsubmit='xmlhttpPost("/solr/select"); return false;'> 
    <p>query: <input name="query" type="text"> 
    <select id="Entity"> 
    <option value="brand1">Universal</option> 
    <option value="brand2">Paramount</option> 
    <option value="brand3">Fox</option> 
    <option value="brand4">Sony</option> 
</select> 
    <input value="Go" type="submit"></p> 

我想下拉值添加到我的面查詢結果爲這樣:

var rsp = eval("("+str+")"); 
var c=document.getElementById("Entity"); 
cat=c.options[c.selectedIndex].value; 
var output=rsp.facet_counts.facet_fields; 
html += "Entity: " + output+'.'+cat; 

我的方面響應發回:實體:[對象對象] .Universal。如何正確地將下拉值添加到響應查詢中,以便Solr實際返回適當的構面值?非常感謝。

回答

2

你肯定會用庫,供您Ajax請求減輕這一問題的複雜性,因爲:

  • ,你可以很容易地做到把它們變成 選擇選項所需的DOM操作
  • 您可以發送與預期任意請求要回一個實際的JSON對象
  • 你能避免使用eval

既然你說你的回答是「實體:[object Object] .Universal」,你能嘗試用console.log(output);來告訴我們什麼是正式返回的對象嗎?還有一種可能性,即通過與對象連接字符串,你搞亂了輸出對象

,如果它實際上是一個對象,你可以反覆訪問它是這樣的:

for(var x in rsp.facet_counts.facet_fields) { 
    //rsp.facet_counts.facet_fields[x] 
} 
+0

感謝您的答覆。 console.log顯示窗口<正確的對象在firebug中:brand1 [「Eve2」,10,「RHCP」,6] brand2 \t [「Black_Keys」,6,「White_Stripe」,5] brand3 [「SoaD」,5, BoB「,3] brand4 \t [」Doors「,3,」Cage_Elephant「,2]。如果我靜態插入4個品牌中的任何一個(rsp.facet_counts.facet_fields.brand1),我會從Solr獲得正確的方面結果。然而,我不知道爲什麼從實體下拉動態添加brand1作爲var與'+'在我身上咳嗽... – Chris

+0

你沒有動態地做它,你強制錯誤地強制類型。製作這些項目的正確方法是創建一個for循環,其中爲每個陣列成員引用了rsp.facet_counts.facet_fields對象。 for(var x in rsp.facet_counts.facet_fields){//用rsp.facet_counts.facet_fields [x]做一些事情:) – Kristian

+0

y ...這樣做,非常感謝這堂課 – Chris