Peter
Peter

Reputation: 11835

Google autocomplete with jquery

i know, there is a gcomplete plugin, but i try to build my own. My problem is that i dont get an answer.

JS

$.get("http://www.google.com/complete/search?qu=chicken", function(data)
 {

     $('body').append("Data Loaded: " + data);

    $.each(data, function(i)
    {                
       $('body').append('- '+data[i]+' <br />');

    });

 });

Hope somebody can help me.

Example http://www.jsfiddle.net/V9Euk/652/

Thanks in advance!
Peter

Upvotes: 1

Views: 214

Answers (2)

Vegard Larsen
Vegard Larsen

Reputation: 13047

Unless your Javascript isn't hosted on the same domain as you are trying to post to, you won't get an answer. In this case, your code is on www.jsfiddle.net, but you are trying to fetch data from www.google.com.

This can easily be worked around by using JSONP instead of JSON. See the dataType parameter of the jQuery.ajax function.

Upvotes: 0

Fermin
Fermin

Reputation: 36111

You need to make your datatype JSONP because you're getting the data from a different domain, for this you'll need to use the AJAX function rather that get

$(function() {

    $.ajax({
        url:"http://www.google.com/complete/search?qu=chicken",
        success:function(data){
            $('body').append("Data Loaded: " + data);
        },
        dataType:'jsonp',
        error:function(){
            alert('error');
        }
    });
});

http://www.jsfiddle.net/V9Euk/653/

Upvotes: 2

Related Questions