0

I am using the jquery autocomplete.when the ajax request completes jquery calls jsonpCallback with the response .can u suggest where to put jsonpCallback(data) function to use the data in $.each

$(".autoComplete").autocomplete({ source: function(req, add){

            $.ajax({
                  type: "GET",
                  url: "http://herokory.herokuapp.com/autocomplete/jsonpCallback/a",
                  dataType:"jsonp"
            });

                var suggestions = [];  

                //process response  
                $.each(data, function(i, val){       //how do I get data variable                         
                suggestions.push(val.value); });

                 add(suggestions);
            }
});  
vishesh
  • 2,007
  • 6
  • 32
  • 67

2 Answers2

1

according to: jquery api you should implement a "success-block"

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});

for your example:

var suggestions = [];  
$(".autoComplete").autocomplete({ source: function(req, add){

            $.ajax({
                  type: "GET",
                  url: "http://herokory.herokuapp.com/autocomplete/jsonpCallback/a",
                  dataType:"jsonp"
                  success: function(data) {

                    $.each(data, function(i, val){                         
                    suggestions.push(val.value); });
                          add(suggestions);
                    }
                 }
            });



});  

// edit: maybe this could help: jsonpcallback

Community
  • 1
  • 1
p0rter
  • 961
  • 2
  • 13
  • 28
  • gives error, says jsonpCallback is not defined.thats what I asked,where to put jsonpCallback – vishesh Aug 22 '12 at 09:13
  • "jsonpCallback is not defined" is an error with the URL you are using. As p0rter is using the URL you provided, we can't know what should be the correct URL. The javascript mentioned here is perfectly valid, which was the original question? – Flater Aug 22 '12 at 09:25
0
    $(".autoComplete").autocomplete({ source: function(req, add){

            $.ajax({
                  type: "GET",
                  url: "http://herokory.herokuapp.com/autocomplete/jsonpCallback/a",
                  dataType:"jsonp"
    success: function(data) 
            {
                alert('Here is my data' +  data);
            }


            });

}); 

You should add a success function into your ajax call meaning that if the GET request is successful do the following this is where you will place your .each loop to iterate through the data.

There is also additional blocks to help you in debugging your code like error blocks etc.

Schokea
  • 708
  • 1
  • 9
  • 28