Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to parse JSON using Jquery

I'm trying to do autosuggestion using solr along with jquery. For this I wrote the code below:

$(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" );
        }

    });
});

I am getting the following error while running in Chrome

Uncaught SyntaxError: Unexpected token : The Json data I am getting is this

{"terms":{"heading":{"answer":24,"ansari":5}}}

I consulted the following link http://jqueryui.com/demos/autocomplete/#remote-jsonp but i am unable to find a solution. Please suggest what I am doing wrong

like image 596
sid Avatar asked Jul 20 '26 17:07

sid


1 Answers

You've (correctly?) specified JSONP to access a Cross Origin resource, but you haven't told Solr that you want it to emit JSONP instead of pure JSON.

Add jsonp: 'json.wrf' to the parameters to $.ajax.

More at http://xplus3.net/2010/09/21/solr-and-jsonp/

like image 171
Alnitak Avatar answered Jul 23 '26 06:07

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!