3 Replies - 429 Views - Last Post: 23 March 2019 - 07:23 PM

#1 sonar87   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 04-February 08

JS Solr query code, nothing displaying

Posted 21 March 2019 - 01:19 PM

I am trying working off of https://wiki.apache.org/solr/SolJSON tutorial. I have put my url for solr in the code, copied from solr admin query result to make sure the query should return something.

I try typing in "title:Asian" into text box but when the button is hit, textbox just clears and nothing in output spot.

I used the dev tools from [F12] key of browser to check console and see there was no errors given there, such as for syntax, so not due to that.

Perhaps I am understanding how the url for query works or should be here? If I leave out local host part as shown I just get error for not specifying local full path. ://> Anyone here try with solr before to know?

<html>
<head>
<title>Solr Ajax Example</title>
<meta charset="UTF-8">
<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);
        }
    };

    var params = getstandardargs().concat(getquerystring());
    var strData = params.join('&');
    self.xmlHttpReq.send(strData);
//document.getElementById("raw").innerHTML = strData;
    return false;
}

function getstandardargs() {
    var params = [
        'wt=json'
        , 'indent=on'
        , 'hl=true'
        ];

    return params;
}
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];
  html += "<br>product name=" + first.name;
  var hl = rsp.highlighting[first.id];
  if (hl.name != null) { html += "<br>name highlighted: " + hl.name[0]; }
  if (hl.features != null) { html += "<br>features highligted: " + hl.features[0]; }
  document.getElementById("result").innerHTML = html;
}

</script>
</head>
<body>

<form name="f1" onsubmit='xmlhttpPost("http://localhost:8983/solr/myCore/select?")'>
  <p>query: <input name="query" type="text">
  <input value="Go" type="submit"></p>

<div id="result"></div>
<p/><pre>Raw JSON String/output: <div id="raw"></div></pre>

</form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: JS Solr query code, nothing displaying

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: JS Solr query code, nothing displaying

Posted 22 March 2019 - 04:41 AM

Throw in a few console.log statements to discover where the code gets to, and print out some of the values of variables. You need to discover where it starts to fail.

Is the main function xmlhttpPost called?
Does the Ajax call complete, and complete successfully?
Do you obtain data, or does the code fail in displaying the data?
etc..
Was This Post Helpful? 0
  • +
  • -

#3 sonar87   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 04-February 08

Re: JS Solr query code, nothing displaying

Posted 23 March 2019 - 02:20 PM

Thank you, did not know about the console.log thing.

So far have figured out...

After fixing port error, it seems I know have a problem with the form and and something wrong with the eval use line.

If I add the xmlhttpPost call to end of script so called automatically and change from getting form input to hard coded value as below its works..

...
}

xmlhttpPost("http://localhost:8983/solr/nutch/select?");

</script>
</head>
<body>

...



function getquerystring() 
{
    console.log("\n entered getquerystring");
  //var form = document.forms['f1'];
  //var query = form.query.value;
  qstr = 'q=' + "text:Asia"; //query;

  console.log("qstr = " + qstr + "\n");
  return qstr;
}



If I don't add the call to xmlhttpPost as above, then when I click the button, console.log output from the updatepage function gets shown, but not the console.logs I put in xmlhttpPost even though xmlhttpPost is only place updatepage is called. :/ and (Ok, seems log statement in if statement, the "about to call updatepage" one is being called, but first log saying entered xmlhttpPost and the strURL: log output not being shown for some reason...) I get error with eval cause the str var is empty, str var being the passed in value of self.xmlHttpReq.responseText which I guess is not set for some reason.


Tried adding "action = "http://localhost:8983/solr/IR.html" method = "POST"" to form parameters as well and did no good.

This post has been edited by sonar87: 23 March 2019 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 sonar87   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 04-February 08

Re: JS Solr query code, nothing displaying

Posted 23 March 2019 - 07:23 PM

Solved! Apparently I had to swap use of ' and " and add "return false" to the onsubmit call, as that did something such to mess it up somehow...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1