简体   繁体   中英

jquery autocomplete running ajax but not displaying results

I have jquery and jquery ui installed on my site, I have

$(document).ready(function(){
        //alert('it ran');
        $('.global_search').autocomplete({ source: "global_search.php", select: function( event, ui){ window.open( ui.item.url ); } });
    });

and when I look in the network tab in chrome I see the result global_search.php?term=54650 ( Note I searched for 54650 )

The response I get from that is

{"150000":{"name":"Event: TestRod08.28.2012","value":"Event: TestRod08.28.2012","link":"event_profile.php?event_id=2939"}}

Which should display "Event: TestRod08.28.2012" and on click should go to event_profile.php?event_id=2939 but the list never show's up? I have other jquery autocomplete's working on the same page and there list's are showing just fine. Any Idea's?

Try

  $('#test').autocomplete({
    source : function(request, callback) {
        $.ajax({
            url : "data.json",
        datatype: 'json'
        }).done(function(data) {
            var results = []

            $.each(data, function(key, value) {
                        results.push({
                                    id : key,
                                    label : value.name,
                    url: value.link
                                });
                    });

            callback(results);
        });
    },
    select : function(event, ui) {
        window.open(ui.item.url);
    }
  });

Demo: Plunker

var options, a;
jQuery(function(){
  options = { serviceUrl:'global_search.php'};
  a = $('.global_search').autocomplete(options);
}); 

You can try that.

Found out that my global_search.php was returning a json object instead of a json array ( I had keyed values ) when I removed the key's it works just fine

Thank you everyone for your help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM