0

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?

4
Contributors
7
Replies
31
Views
4 Years
Discussion Span
Last Post by Dani
0

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.

0

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.

0

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.

Edited by pritaeas

0

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 :)

2

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.

Edited by Airshow

Votes + Comments
Thank you!!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.