2
votes

I am working to get response from SOLR using jquery. When I use the below code I got the error saying Typeerror:data is null

When looked at the code in firebug, data in on_data function is null. I think I am missing something in the Solr URL.The URL I am using is http://xxxx.xxx.xxxx.xxx/xxx_xxx/core0/selectcore0/select/?q=%3A&version=2.2&start=0&rows=10&indent=on&wt=json. Can you please take a look at my code, and also suggest me the URL style in the code

<html>
<head>
    <title>Solr Search</title>
</head>
<body>
    <h3>Solr Search</h3>

    Query: <input id="query" /> 
    <button id="search">Search</button>
    <hr/>
    <div id="results">
    </div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    function on_data(data) {
        $('#results').empty();

        var docs = data.response.docs;
        $.each(docs, function(i, item) {
            $('#results').prepend($('<div>' + item.name + '</div>'));
        });

        var total = 'Found ' + docs.length + ' results';
        $('#results').prepend('<div>' + total + '</div>');
    }

    function on_search() {
        var query = $('#query').val();
        if (query.length == 0) {
            return;
        }

        var url='http://xxxx.xxx.xxxx.xxx/xxx_xxx/core0/selectcore0/select/?q='+query+'&version=2.2&start=0&rows=50&indent=on&wt=json';
        $.getJSON(url, on_data);
    }

    function on_ready() {
        $('#search').click(on_search);
        /* Hook enter to search */
        $('body').keypress(function(e) {
            if (e.keyCode == '13') {
                on_search();
            }
        });
    }

    $(document).ready(on_ready);
</script>

3

3 Answers

4
votes

Here is the working version of your code, and list of changes: 1) added wrf: adds a wrapper-function around the JSON response, useful in AJAX with dynamic script tags for specifying a JavaScript callback function 2) added callback: need to use JSONP instead of an ordinary JSON request, due to JavaScript’s cross-domain security restrictions.

<html>
<head>
    <title>Solr Search</title>
</head>
<body>
    <h3>Solr Search</h3>

    Query: <input id="query" /> 
    <button id="search">Search</button>
    <hr/>
    <div id="results">
    </div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    function on_data(data) {
        $('#results').empty();
        var docs = data.response.docs;
        $.each(docs, function(i, item) {
            $('#results').prepend($('<div>' + item.name + '</div>'));
        });

        var total = 'Found ' + docs.length + ' results';
        $('#results').prepend('<div>' + total + '</div>');
    }

    function on_search() {
        var query = $('#query').val();
        if (query.length == 0) {
            return;
        }

        var url='http://xxxx.xxx.xxxx.xxx/xxx_xxx/core0/selectcore0/select/?q='+query+'&version=2.2&start=0&rows=50&indent=on&wt=json&callback=?&json.wrf=on_data';
        $.getJSON(url);
    }

    function on_ready() {
        $('#search').click(on_search);
        /* Hook enter to search */
        $('body').keypress(function(e) {
            if (e.keyCode == '13') {
                on_search();
            }
        });
    }

    $(document).ready(on_ready);
</script>
2
votes

Since you are using $.getJSON, you might need to add &wt=json to your query.

So your query should be: http://xxxx.xxx.xxxx.xxx/xxx_xxx/core0/selectcore0/select/?q=search_query&version=2.2&start=0&rows=10&indent=on&wt=json

By default Solr gives XML response. You need to specify if you need JSON response by adding &wt=json

Mode detail: http://wiki.apache.org/solr/SolJSON

0
votes

I have installed solr 5.2.1: item.name in the function on_data is not provided anymore. I used item.id and it worked.