I have an API that supports both JSON and JSONP. Obviously, being a data provider, I want the people who request my data to cache it.

However, jQuery's $.ajax() function says the following about their caching feature:

cache (default: true, false for dataType 'script' and 'jsonp')
Type: Boolean
If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

Why does jQuery go out of its way to force a cashbuster when requesting jsonp? Is there a technical/philosophical reason to cachebust jsonp but not json?

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

Obviously, being a data provider, I want the people who request my data to cache it.

Since JQuery mention about Cache and it won't let you do that.

You just want members to used your data to cache?

How about saving the cache in the database then used JQuery Ajax cache?

I think that consider using your data to cache.

I don't know it's just a different solution.

I think you're misunderstanding. I'm asking why jQuery has not to cache as the default for jsonp even though it has caching as the default for all other get requests.

A little lower in the manual (dataType) it says this:

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

So my guess is that this line:

cache (default: true, false for dataType 'script' and 'jsonp')

only refers to the default setting, when omitted. As for "why?" the jQuery forum may get you an answer.

Yes, I know that's how it works. I was hoping someone on DaniWeb could tell me why instead of having to go to the jQuery forum :)

Hi Dani,

The jQuery documentation for dataType:'script' includes the statement :

Note: This [setting the cache option to true] will turn POSTs into GETs for remote-domain requests.

Although the section for dataType:'jsonp' doesn't include the same note, I rather imagine that the same applies - after all jsonp is a specialised form of script.

If that's correct, then for me it logically follows that, AJAX requests for jsonp and script should have caching set to false by default in order to allow POST by default. Otherwise, the default case would convert POST to GET, which would be thoroughly confusing - the coding forums would all be full of questions asking "why doesn't my script/jsonp request work?". It's arguabley still confusing with a default of false, but significantly less so.

Maybe the question then becomes, "why is caching set to true for all other types of AJAX request?". I expect (without checking) that the answer is historical - ie that jQuery added script/jsonp requests some time after the default of true was established for other datatypes.

commented: Thank you!! +15

Thanks Airshow! That makes perfect sense :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.