14

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?

Thiago
  • 1,547
  • 3
  • 25
  • 40

2 Answers2

20

This will disable caching for jQuery ajax:

jQuery.ajaxSetup({ cache: false });
Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • Whether to cache or not is usually better left up to the service serving the data because it knows how long before the data is stale. This solution will force all clients of the service to put this line into their client code. If that is not acceptable, the service needs to be modified to include the appropriate headers in its response which indicate to clients that they should not cache. – Jesse Webb May 24 '11 at 22:57
  • 4
    @Gweebz. Server-side and client-side caching are quite different. Nobody is forcing clients to so anything in this case. It just happens that IE clients like to cache more aggresively than other clients, so IE needs this line of code. Server-side caching can, and should, be configured independent of this decision. As far as controlling client-side caching with server-side created headers, I don't see the upside. It would just create a tight coupling between server and client side code which you yourself argue is bad. – Milimetric May 24 '11 at 23:05
  • For more in-depth discussion about this, anyone could refer here: http://dotnet.dzone.com/articles/output-caching-aspnet-mvc – Rosdi Kasim May 14 '13 at 17:48
9

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.

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
  • 1
    +1 This should be the correct answer. The HTTP spec for caching is geared toward how the origin wants its content managed in terms of caching. Changing it in javascript won't change how intermediate caching proxies work. So the best thing to do is specify the proper no-cache headers on the server so it works in all cases. – Jerico Sandhorn Jan 15 '14 at 19:40