Bug Tracker

Opened 9 years ago

Closed 9 years ago

#8764 closed bug (wontfix)

Regression in getJSON

Reported by: patrick.wolf@… Owned by: jaubourg
Priority: high Milestone: 1.next
Component: ajax Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:

Description

Using $.getJSON for a JSONP request always adds a parameter &_=somenumber to the URL which can break the request, because not all server system do expect a parameter called _ in the request.

Using the example from http://api.jquery.com/jQuery.getJSON/ demonstrates the problem when using jQuery 1.5.2 and viewing the request using FF and Live HTTP Header. Running the same example in 1.4.4 doesn't add that parameter.

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "cat",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    alert('test');
  });

Change History (2)

comment:1 Changed 9 years ago by Rick Waldron

Component: unfiledajax
Owner: set to jaubourg
Priority: undecidedhigh
Status: newassigned

comment:2 Changed 9 years ago by jaubourg

Resolution: wontfix
Status: assignedclosed

It's because default for cache option is false in the context of a script request (which jsonp is). You have several solutions around this:

  • use ajax
      $.ajax( "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {
        dataType: "jsonp",
        cache: true,
        data: {
          tags: "cat",
          tagmode: "any",
          format: "json"
        },
        success: function(data) {
          alert('test');
        }
      } );
    
  • set cache option to true by default using ajaxSetup:
      $.ajaxSetup({
        cache: true;
      });
    
  • make sure cache option is true by default for script using a prefilter
      $.ajaxPrefilter( "script", function( options, originalOptions ) {
        if ( originalOptions.cache == null ) {
          options.cache = true;
        }
      } ); 
    
  • create a prefilter to deal with the service that poses a problem:
      $.ajaxPrefilter( "script", function( options ) {
        if ( /^http:\/\/api\.flickr\.com\//.test( options.url ) ) {
          options.cache = true;
        }
      } ); 
    

Lots of alternatives here.

Note: See TracTickets for help on using tickets.