Skip to main content

Bug Tracker

Side navigation

#8764 closed bug (wontfix)

Opened April 04, 2011 02:16PM UTC

Closed April 07, 2011 02:55PM UTC

Regression in getJSON

Reported by: patrick.wolf@oracle.com 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');
  });

Attachments (0)
Change History (2)

Changed April 07, 2011 01:37PM UTC by rwaldron comment:1

component: unfiledajax
owner: → jaubourg
priority: undecidedhigh
status: newassigned

Changed April 07, 2011 02:55PM UTC by jaubourg comment:2

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.