2

I'm creating an asp.net web application in c# and have hit a slight problem when viewing a page on the iPhone 4 and 5 (not sure about other mobiles as I've had no reports from other mobile users).

When viewing the page on a desktop browser it all works as it should. What happens is that the user enters their email address in a text box and click a button. I have some Javascript JQuery on the button that calls a web method via ajax, the web method returns an list of IDs and Names in json - the button then populates a drop down box on the web page with this information.

The user can add information to the database via a different part of the site which when they click the above button the list that is returned should be larger and include the new information. However I've noticed on the iphone 5 that when I've gone back to the page the ajax call doesn't seem to have taken place (it briefly shows a popup when it happens) and the drop down box doesn't contain the new information in the database. I believe it's simply displaying information from the phones CACHE.

If I go into the safari settings and delete cookies & data / web data, then go to the webpage, voila all the drop down is populated and the ajax call works (please wait popup shows). I have tried adding the below line of code to my master page on page_load, but it's not worked.

Response.Cache.SetNoStore();

Is there anything else I need to do? As I say, the site works for desktop browsers, and I'm not a mobile developer (I don't even use mobiles for web browsing) so not sure what the problem is or how I can fix it in my code.

Many thanks for any help/advice.

Harag
  • 1,532
  • 4
  • 19
  • 31
  • Try to add a querystring variable to the address of your ajax call. Change the variable for each request so the browser won't think that it is cachable. Principle described (but it is not exactly the same) in my answer to this question: http://stackoverflow.com/questions/14231872/force-app-to-refresh-image-from-url/14232252#14232252 – mortb Jan 11 '13 at 09:10

1 Answers1

0

Your theory that this is caching related is correct - iOS 6 caches a lot more aggressively where there's no cache-control header supplied, including POSTs. See Is Safari on iOS 6 caching $.ajax results? for more info.

We had the same issue and the approach we took for this was to add an HttpModule to the site that always sets this header for any POST request:

public class DisablePostCachingModule : IHttpModule
{
    private HttpApplication _context;
    public void Init(HttpApplication context)
    {
        _context = context;
        _context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }

    public void Dispose()
    {
    }

    private void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.HttpMethod == "POST")
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
    }
}

We hooked this up in Web.config as below and it did the trick for us:

<configuration>
  <system.webServer>
    <modules>
      <add name="DisablePostCachingModule" type="Full.Namespace.To.DisablePostCachingModule, AssemblyName" />
    </modules>
  </system.webServer>
</configuration>

Note if you aren't using IIS7's Integrated Mode you can use <system.web> instead:

<configuration>
  <system.web>
    <httpModules>
      <add name="DisablePostCachingModule" type="Full.Namespace.To.DisablePostCachingModule, AssemblyName" />
    </httpModules>
  </system.web>
</configuration>

Obviously in both cases above, you would need to update Full.Namespace.To to whatever namespace you'd used for the module, and AssemblyName to the name of the assembly in which the module resides. So for example:

<add name="DisablePostCachingModule" type="Acme.HttpModules.DisablePostCachingModule, Acme.Core" />
Community
  • 1
  • 1
Luke Bennett
  • 32,786
  • 3
  • 30
  • 57
  • Many thanks for the above, I will certainly add it in. The safari/ajax link looks very positive at first glance so will digest that this weekend. I can't test it fully again until Monday, so if I get it working I'll certainly accept your answer. – Harag Jan 11 '13 at 10:18
  • Can you please give me an example of how you hook this up in the web.config? Apologies, this is new to me. – Harag Jan 11 '13 at 13:58
  • Much appreciated. Makes more sense on how to implement it. Many thanks – Harag Jan 12 '13 at 22:57
  • I've done the change over the weekend and tested it this morning seems to have fixed the problem. I've also implemented a line in the ajax setup from the link you sent me. Thanks – Harag Jan 14 '13 at 09:18