无法使用Jquery分析JSON

Unable to parse JSON using Jquery

本文关键字:分析 JSON Jquery      更新时间:2023-09-26

我正在尝试使用solr和jquery进行自动建议。为此,我写了以下代码:

$(function(){

    $( "#artist" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: 'http://localhost:8983/solr/terms?terms.fl=heading&terms.prefix='
                +request.term+'&wt=json&json.nl=map',
                dataType: "jsonp",
                data: {
                    q: request.term,
                    rows: 10, 
                    omitHeader: true,
                },
                success: function( data ) {
                    response( $.map( data.terms.heading, function( item ) {
                        return {
                            label: item,
                            value: item
                        }
                    }
                    )
                    );
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.label :
                "Nothing selected, input was " + this.value);
        },
        open: function() {
            $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    });
});

我在Chrome 中运行时出现以下错误

未捕获的语法错误:意外的标记:我得到的Json数据是这个

{术语:{标题:{答案:24,ansari:5}}

我查阅了以下链接http://jqueryui.com/demos/autocomplete/#remote-jsonp,但我找不到解决方案。请建议我做错了什么

您已经(正确地?)指定了JSONP来访问跨源资源,但您没有告诉Solr您希望它发出JSONP而不是纯JSON。

jsonp: 'json.wrf'添加到$.ajax的参数中。

更多信息,请访问http://xplus3.net/2010/09/21/solr-and-jsonp/