Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json is being cached incorrectly

Hy!

My JS is requesting a JSON from controller to edit an existing object, a populated dropdownlist.

Then, the View send the actual values from my autosuggest dropdown, to lately the new value be compared to the old one and the new values be stored.

It is like a list of Persons. When I load the page, there is some persons in my ddl and I can add or delete persons.

This is my controller:

    [HttpGet]
    public JsonResult JSON(int order)
    {
        IEnumerable<Person> persons = dataServ.Envolvidos.GetPerson( order )
        return this.Json( new { Result = persons }, JsonRequestBehavior.AllowGet );
    }

And my Json call:

$.getJSON("/Order/JSON", { order: $("#Id").val() }, function (data) {
   ...
});

Everything is going fine, except by the point that I.E. is caching this JSON, and when I send the new values and come back to the edit the page again, the old values are there instead of the new. But the new values is stored on database, like should be.

I tested on Chrome and Firefox and after I edit and come to edit again, it's done a new json call and the new values are there, different from I.E.

Am I missing something? What I should do to JSON result don't be cached?

like image 759
Thiago Avatar asked May 24 '11 18:05

Thiago


People also ask

Do JSON files get cached?

If the JSON file is being read and used by PHP on the server then it won't (can't!) be cached by the user's browser. The resulting pages might be cached, though, either by the browser or by some intervening proxy, or both.

Does browser cache JSON response?

Request Headers 2nd request: Thx for response. Yes you can cache JSON responses.

Does Cloudflare cache JSON files?

By default, Cloudflare does not cache JSON files without being forced by a Page Rule.


2 Answers

This will disable caching for jQuery ajax:

jQuery.ajaxSetup({ cache: false });
like image 98
Milimetric Avatar answered Sep 24 '22 00:09

Milimetric


I believe IE caches JSON requests by default, unlike the other browsers. You will have to manually include the appropriate headers to tell the response not to be cached. This won't hurt the existing browsers which already don't cache, it will just be more explicit.

like image 28
Jesse Webb Avatar answered Sep 24 '22 00:09

Jesse Webb