Mayank Kapahi
Mayank Kapahi

Reputation: 326

how to call autocomplete inside ajax function

autocomplete inside ajax not working...help

$.ajax({
    url: 'url',
    type: 'GET',
    dataType :'jsonp',
    async: false,
    crossDomain:true,
    success: function(data) {
        var resp = data.response;
        var availableTags="[";
        for (i = 0; i <= 10; i++) {
            var postSub = resp.messages.message[i].subject.$;
            if (i  < 10) {
                availableTags += postSub + ",";
            }
            else {
                availableTags += postSub;
            }
        }
        availableTags += "]";
        $("#tags").autocomplete({
            source: availableTags
        });
    }
});

Upvotes: 1

Views: 623

Answers (2)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

It wont populate because availableTags is a string, not array type. You should make it as an array. Change your code like,

    var availableTags = "[";
for (i = 0; i <= 10; i++) {
    var postSub = resp.messages.message[i].subject.$;
    if (i < 10) {
        availableTags +="\"" +postSub + "\",";
    }
    else {
        availableTags +="\""+ postSub+"\"";
    }
}
availableTags += "]";
availableTags=eval(availableTags);

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

You don't need to make the AJAX request yourself, as the source parameter takes a URL as a string and will do all of this for you. Try this:

$("#tags").autocomplete({
    source: 'yourURL'
});

Autocomplete source param API

Upvotes: 0

Related Questions