AJAX Cache Issue

edited December 2010 in Modules
Hey everyone,

In developing my Module, I have forms submitted with AJAX requests and when the result is loaded it seems the output is cached. If the page is refreshed everything is updated. I know CI had this problem and the solution I found then was to not cache the page with the following code

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
$this->output->set_header("Pragma: no-cache");

but that doesn't seem to work, is there a different way to achieve this?

Thanks in advance.

-Justin

Comments

  • edited December 2010
    Is it a page made in the admin? Is cache enabled for it?

    I've had no problems with CI and Ajax in the past.

    Are you returning JSON and pushing to the DOM or?
  • edited 11:25AM
    Sorry I wasn't a little more specific. Now that I think about it, it wasn't CI that had the cache issue it was IE, but i'm having this problem with all browsers now.

    I'm building forms in a controller that are outputted to a view. There are 3 forms that show up when the previous one has been submitted. When the form is submitted the controller is reloaded with the same view it's just creating the form after each request. So the whole page is returned and I just update the div that holds the forms. To see how it works with just CI you can go here:

    https://www.gmvoices.com/asteriskstore/order

    I'm trying to recreate this whole store as a module in the CMS. Everything works right when I don't use AJAX and if I refresh the page, it is updated.

    -Justin
  • edited 11:25AM
    Have you tried turning off cache for your AJAX requests. I'm not sure if you are using jQuery or not, but if so, you may want to look here:
    http://api.jquery.com/jQuery.ajax/

    Also, appending a random string or timestamp on the request as a query string param should create a unique request to prevent caching.
  • edited 11:25AM
    Yes, I am using jQuery and I have turned off cache, but it's still doing it. As far as a query string I thought CI doesn't like those urls, I get a 404 error.
  • edited 11:25AM
    That's true... there are ways around (you could try putting the query string after a slash and use 2 query string params instead of 1 e.g. https://www.gmvoices.com/asteriskstore/order/?nocache=12345&x=1) however, you may have better luck with using an extra URI segment parameter seen as a parameter to your controller method instead of a query string. However, caching turned off in jquery should be enough I would think.

    Hmm... I'm not sure. Anyone else have any ideas?
  • edited 11:25AM
    I'm also using post so it shouldn't cache anyways, but the response is always one behind. I have to click the button twice to finally see the result. I did append a time stamp as a parameter to my controller and it didn't help either.
  • edited 11:25AM
    So i figured out what was going on finally.

    If something is posted it's saved in the user's session and I was checking the session before i was setting it. so as a result i was always getting the previous posted info. It's working perfectly now, man I feel pretty dumb. Thanks though.

    -Justin
  • edited 11:25AM
    No problem... glad you figured it out.
Sign In or Register to comment.