2012-01-18 155 views
3

我正在尝试使用solr和jquery一起进行自动建议。为此,我写了下面的代码:意外的令牌: JSON数据我无法使用jquery解析JSON

$(函数(){

$("#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

未捕获的SyntaxError运行时我得到的是这种

{ “条款”:{ “标题”:{ “答案”:24, “安萨里”:5}}}

我咨询下面的链接http://jqueryui.com/demos/autocomplete/#remote-jsonp但我无法找到解决方案。请建议我做错了什么

+0

您发布的代码有错误的“});”最后。也应该dataType:不是“json”而不是“jsonp”; – CBusBus 2012-01-18 08:15:43

+0

可能试试$ .map(data.d.terms.heading,函数(item) – 2012-01-18 08:18:03

+0

可以发布jQuery字符串吗? – 2012-01-18 08:39:07

回答

3

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

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

更多在http://xplus3.net/2010/09/21/solr-and-jsonp/

+0

感谢您的回答。它的工作原理 – sid 2012-01-18 12:06:37

+0

它的jsonp:'json.wrf'而不是json:'json .wrf“。附加链接是正确的,但引用的答案是错误的 – 2015-10-16 16:51:24

+0

@BalajiNatarajan谢谢 - 更正 – Alnitak 2015-10-17 10:09:16