javascript - 使用 jquery 调用从 SOLR 检索结果

标签 javascript jquery solr

我正在努力使用 jquery 从 SOLR 获得响应。当我使用下面的代码时,我收到错误提示 Typeerror:data is null

查看firebug 中的代码,on_data 函数 中的数据为空。我想我在 Solr URL 中遗漏了一些东西。我使用的 URL 是 http://xxxx.xxx.xxxx.xxx/xxx_xxx/core0/selectcore0/select/?q= %3A&version=2.2&start=0&rows=10&indent=on&wt=json。 能否请你看看我的代码,并建议我代码中的 URL 样式

<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>

最佳答案

这是您的代码的工作版本和更改列表: 1) 添加了 wrf: 在 JSON 响应周围添加了一个包装函数,在 AJAX 中很有用,带有用于指定 JavaScript 回调函数的动态脚本标签 2) 添加回调:由于JavaScript的跨域安全限制,需要使用JSONP而不是普通的JSON请求。

<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>

关于javascript - 使用 jquery 调用从 SOLR 检索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16742610/

相关文章:

javascript - 图片选择Vuejs

javascript - 在 React.js 中选择随机样式的组件

java - 嵌入solr有什么问题

java - Solr查询: replacing whitespace with +

solr - SOLR排名中权重的确切含义

javascript - 主干应用 View 中的 “Uncaught TypeError: undefined is not a function”

javascript - 循环从二维数组创建对象键和值

javascript - 如何设置类组合的CSS规则?

jquery-mobile - JQueryMobile监听点击

jquery - jquery mobile 中一键关闭和打开弹出窗口