Narendra Rajput
Narendra Rajput

Reputation: 711

Iterate over the data to show Autocomplete suggestions

This is an API which returns results in an array, for auto suggestions

For Eg. if I query "google" I get the results as follows

["google","google maps","google translate","google earth","google images","google docs","google voice","google scholar","google chrome","google calendar",]

You can try it yourself here

Here's my code that I m using to query this API but it doesnt seem to return results.

Here's the code that I m using for it

     $(function() {

    $( "#city" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "q.php",
                dataType: "json",
                data: {
                    "q" : request.term
                },
                success: function( data ) {
                    response(data[1]);
                }
            });
        },
        minLength: 2
    });
});

I dont understand where am I going wrong in the code Please correct me ! Because it doesnt seem to be working as I wanted

Edit: Accessing the Data from the same server

Upvotes: 0

Views: 267

Answers (3)

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

You can read here about using jQuery autocomlete for cross-domain environment: http://1300grams.com/2009/08/17/jquery-autocomplete-with-json-jsonp-support-and-overriding-the-default-search-parameter-q/

Also you need to add jquery.autocomplete.js to your jsFiddle environment.

Upvotes: 0

Gaurav Shah
Gaurav Shah

Reputation: 5279

This might be helpful

$(document).ready(function() {
$.getJSON('http://twitter.com/users/usejquery.json?callback=?', function(json) { //get information about the user usejquery from twitter api
$('#twitter_followers').text(json.followers_count); //get the follower_count from the json object and put it in a span
});
});

Look for something called as cross-domain ajax. http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide

Upvotes: 0

Roman Pominov
Roman Pominov

Reputation: 1433

You forgot to add jquery-ui library in your fiddle. But if you do code will not work anyway, because you can't access data from another domain via ajax request. Only from same domain on what js code executes.

Upvotes: 1

Related Questions