I need to get a large amount of data back from a server via ajax. It will take a bit of time to get to t开发者_如何学Pythonhe browser. The second time the user goes back to same web page I don't want them to have to download the data via ajax again. Is there anyway Javascript can write the json data to the browsers cache or filesystem reliably?
Cheers
Rich
That's when you can take advantage of having a RESTful service.
If your requests are made via GET
and are idempotent (eg: same queried URL will always yield the same response), the browser will cache the response.
See: http://ajaxpatterns.org/RESTful_Service
As Tim says, localStorage is a good option, however not all browsers support it. As a fallback, you could store the JSON text in the session.
Have you looked at the localStorage spec?
I think if the response has the proper headers, the browser will cache it like any other page. I found this tutorial very informative.
http://www.mnot.net/cache_docs/
You can automatically store the AJAX response into localStorage and use either the network reply or the cached version, as explained here: http://myok12.wordpress.com/2011/08/19/building-an-almighty-data-retrieval-system-for-all-html5-webapps/
精彩评论