zoukankan      html  css  js  c++  java
  • solr json ajax

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

    1.json.jsp关键的html内容
      <form action="select/" name="f1" method="get" onsubmit="xmlhttpPost('/solr/select'); return false;" >
          Chenlb: 
          
    <input type="text" name="q" size="80" value="文档">
          
    <input name="start" type="hidden" value="0">
        
    <input name="rows" type="hidden" value="10">
        
    <input name="indent" type="hidden" value="on">
        
    <input name="wt" type="hidden" value="">
          
    <input type="button" value=" 搜 索 " onclick="xmlhttpPost('/solr/select');">
          
    <input type="button" value=" get json " onclick="document.forms['f1'].wt.value='json';document.forms['f1'].submit();">
          
    <input type="button" value=" get xml " onclick="document.forms['f1'].wt.value='';document.forms['f1'].submit();">
      
    </form>
      
    <div style="background-color: #ccccff; height: 15px;"></div>
      
    <p>
        
    <div id="header"></div>
        
    <div id="response"></div>
        
    <table id="docs" class="tab" cellspacing="1">
            
    <tr height="25" style="background-color: #cccccc; color: #0000ff;">
                
    <td>作者</td>
                
    <td>简介</td>
                
    <td>标题</td>
                
    <td>score</td>
            
    </tr>
        
    </table>

    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<docs.length; i++) {
            tr 
    = tab.insertRow(-1);
            td 
    = tr.insertCell(-1);
            td.innerHTML 
    = docs[i].author;
            
            td 
    = tr.insertCell(-1);
            td.innerHTML 
    = docs[i].introduce;
            
            td 
    = tr.insertCell(-1);
            td.innerHTML 
    = docs[i].title;
            
            td 
    = tr.insertCell(-1);
            td.innerHTML 
    = docs[i].score;
        }
       }

    3.现在可以搜索了。
    http://localhost:8080/solr/json.jsp
  • 相关阅读:
    -webkit-user-select
    防火墙配置
    apache+tomcat集群部署笔记
    项目管理理念
    用plsql 导入导出oracle表结构数据
    Pair programming
    [整理]Linux压缩与解压缩命令整理。
    [转]虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
    [原创]VM虚拟机安装centos6.4详细图文教程
    [转]z-order引出的问题
  • 原文地址:https://www.cnblogs.com/wycg1984/p/1567584.html
Copyright © 2011-2022 走看看