1

I use JSONP on a client to get data from a server using a WCF service that can return results using HTTP GET (It gets a 'callback' parameter which is a 'function name' and returns callback({data}), you know... JSONP).

Everything works, but if I enable caching (using 'AspNetCacheProfile')on one of the service's operations - then something is wrong, and I'm not sure what...

The way I get the JSONP is by using a function I picked up some time ago from a question here on SO (http://stackoverflow.com/questions/2499567/how-to-make-a-json-call-to-a-url)

function getJSONP(url, success) {
  var ud = 'fc_' + + Math.floor(Math.random()*1000000),
      script = document.createElement('script'),
      head = document.getElementsByTagName('head')[0]
          || document.documentElement;

  window[ud] = function(data) {
      head.removeChild(script);
      success && success(data);
  };

  script.src = url.replace('callback=?', 'callback=' + ud);
  head.appendChild(script);
}

This creates a random id ('fc_xxxx') then assigns it as a function inside the window object, then use it as the 'callback' parameter for the url of the dynamic javascript that is injected to the document, and then the 'ud' function runs, removes the script and calls the 'success' callback function with the received data.

When using it on normal uncached operations from the service, it usually takes about 200ms to get back the response, and it works ok.
The cached responses takes ~10ms - and I get an error that the 'fc_XXXXX' function is undefined.

It's as if the response is "too fast" for it.

I also tried using jQuery.getJSON() - and, again the callback doesn't trigger.
In all cases when I look at the network traffic in Firebug - I can see the GET request, and I can see that the right data does in fact gets returned.

Does anybody have an idea how can I make it work right with the cached responses...?

Yuval A.
  • 5,849
  • 11
  • 51
  • 63
  • Looks OK to me. Are you absolutely sure the server returns the right response when cached? Can you post some output from Dragonfly/Firebug/whatever you use? –  Apr 05 '12 at 12:01

1 Answers1

0

I got it! The name of the response-function is different on each call (on both my manual jsonp implementation and that of jQuery).
The name of the function is part of the response from the server (as that's part of how jsonp works...). So, if the response is a cached response - it will actually return the old name of the function (which will no longer exist on the client's context).
So I just need to give a constant name for the callback function in this case and it should bee fine. :)

Yuval A.
  • 5,849
  • 11
  • 51
  • 63