lanxi256 发表于 2015-7-17 07:47:45

solr json ajax

用ajax去请求solr服务。返回json,然后解释。让solr返回json的参数是wt=json。然后javascript用evel()解释成对象。
我的solr会返回:auother,title,introduce这几个域。先创建一个jsp或html,如:json.jsp

1.json.jsp关键的html内容


       Chenlb:
      
      
   
   
   
      
      
      
   
   
   
   
   
   
         
             作者
             简介
             标题
             score
         
      
2.javascript部分

function xmlhttpPost(strURL) {
   var xmlHttpReq = false;
   var self = this;
   if (window.XMLHttpRequest) { // Mozilla/Safari
         self.xmlHttpReq = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) { // IE
         self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
   }
   
   var params = getstandardargs().concat(getquerystring());
   var strData = params.join('&');
   
   var header = document.getElementById("response");
   header.innerHTML = strURL+'?'+strData;

   self.xmlHttpReq.open('get', strURL+'?'+strData+'&time='+new Date().getTime(), true);
   self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   self.xmlHttpReq.onreadystatechange = function() {
         if (self.xmlHttpReq.readyState == 4) {
             updatepage(self.xmlHttpReq.responseText);
         }
   }
   self.xmlHttpReq.send(null);
}

function getstandardargs() {
   var params = [
         'wt=json'
         , 'indent=on'
         , 'hl=true'
         , 'hl.fl='
         , 'fl=*,score'
         , 'start=0'
         , 'rows=10'
         ];

   return params;
}
function getquerystring() {
   var form = document.forms['f1'];
   var query = form.q.value;
   qstr = 'q=' + encodeURI(query);    //escape
   return qstr;
}

// this function does all the work of parsing the solr response and updating the page.
function updatepage(str){
   //document.getElementById("response").innerHTML = str;
   var rsp = eval("("+str+")"); // use eval to parse Solr's JSON response
   parse(rsp);
}

function parse(j) {
   var header = document.getElementById("header");
   var rh = j.responseHeader;
   var header_str = " 搜索: """+rh.params.q+""", 花了: "+rh.QTime+"ms, 共显示: "+j.response.numFound+"条记录, 总共有: "+rh.params.rows;
   header.innerHTML = header_str;
   var docs = j.response.docs;
   var tab = document.getElementById("docs");
   for(; tab.rows.length >1; ) {
         tab.deleteRow(-1);
   }
   var tr;
   var td;
   for(var i=0; i
页: [1]
查看完整版本: solr json ajax