Youss
Youss

Reputation: 4212

Does JSONP work with jquery 1.4

I have this code :

$(document).ready(function(){

    $("#search_input").autocomplete({
      source: function(request, response) {
        $.ajax({
          url: 'http://query.yahooapis.com/v1/public/yql',
          dataType: 'JSONP',
          data: {
            format: 'json',
            q: 'select * from xml where url="http://google.com/complete/search?hl=nl&output=toolbar&q=' + encodeURIComponent(request.term) + '"'
          },
          success: function(data) {
            response($.map(data.query.results.toplevel.CompleteSuggestion, function(item) {
              return { label: item.suggestion.data, value: item.suggestion.data };
            }));
          }
        });
      }
    });

    });

For some reason it doesn't work with Jquery 1.4 but it does with 1.7 Is this becouse JSONP was not introduced with 1.4? How can I make it work with 1.4? Here is the Fiddle JsFiddle

Upvotes: 0

Views: 344

Answers (1)

Rob W
Rob W

Reputation: 349062

Demo: http://jsfiddle.net/TnPRA/2/

You've got two problems:

  1. Wrong usage of JSFiddle.
    • You included the resources at the upperleft section, while loading Mootools.
      The correct usage is to select jQuery ____ at Use Choose framework,
    • You also added the script to the HTML and script section (= running the same code twice).
  2. The critical part of your code is caused by jQuery itself:
    In jQuery 1.4, the response is a string. To get the code to work in older jQuery versions, add one line to the success handler:

    success: function(data) {
      if (typeof data == 'string') data = $.parseJSON(data);
    

Upvotes: 2

Related Questions