Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Solr Wiki" for change 
notification.

The following page has been changed by YonikSeeley:
http://wiki.apache.org/solr/SolAjax

The comment on the change is:
simple AJAX example using Solr's JSON output

New page:
== Using Solr's JSON output for AJAX ==
Solr's JSON output makes parsing the response in !JavaScript simple.  Since 
JSON is a subset of !JavaScript, one
can use the built-in !JavaScript parser to parse a JSON message.

{{{ var rsp = eval("("+jsonResponseString+")"); }}}

Here is a simple functional example.  To install it, place in somewhere 
accessible in the same server running Solr. For example save the text below to 
webapps/ajax/ajax.html and browse to http://localhost:8983/ajax/ajax.html

{{{
<html>
<head>
<title>Solr Ajax Example</title>
<script language="Javascript">
// derived from http://www.degraeve.com/reference/simple-ajax-example.php
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");
    }
    self.xmlHttpReq.open('POST', strURL, 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(getquerystring());
}

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

// this function does all the work of parsing the solr response and updating 
the page.
function updatepage(str){
  document.getElementById("raw").innerHTML = str;
  var rsp = eval("("+str+")"); // use eval to parse Solr's JSON response
  var html= "<br>numFound=" + rsp.response.numFound;
  var first = rsp.response.docs[0];
  hlml += "<br>product name="+ first.name;
  var hl=rsp.highlighting[first.id];
  if (hl.name != null) { html += "<br>name highligted: " + hl.name[0]; }
  if (hl.features != null) { html += "<br>features highligted: " + 
hl.features[0]; }
  document.getElementById("result").innerHTML = html;
}
</script>
</head>

<form name="f1">
  <p>query: <input name="query" type="text">  
  <input value="Go" type="button" 
onclick='JavaScript:xmlhttpPost("/solr/select?wt=json&highlight=true&highlightFields=name,features&indent=on")'></p>
  
<div id="result"></div>
<p/><pre>Raw JSON String: <div id="raw"></div></pre>
</form>
</body>
</html>
}}}

Reply via email to