1

I am using smoothState.js and I am having an issue with submitting a form and then reloading (refreshing) the page.

When I submit the first time it works as expected. The page refreshes with the new/changed data in the inputs. But when I submit it the second time, the data apparently gets posted, but on the refresh the old unchanged data gets loaded. It is only after I do a manual refresh (click on the refresh button in the browser again) is when the new changed data appears. This only happens on the second/third/fourth etc... submit on the same form to the same page.

$('#companyProfile').submit(function(e) { 
     e.preventDefault();
     if ( $(this).parsley().isValid() ) {
      var $form = $("#companyProfile");
      $.ajax({
       type     : "POST",
       async    : true,
       url      : '/customerProfile.do?',
       data     : $form.serialize(),
       success  : function(data) {
       console.log("Submit happened");
       var smoothState = $('#main-cont').smoothState().data('smoothState');
        smoothState.load(window.location.reload);
       }
      });
      return false;
     }
 });

I have added a console.log("Submit happened"); and what happens with that is that the first time the post is submitted the source is viewOrderSettings.jsp as it should be, but the second time it is from VM34065:11 as seen in the screenshot below. If that helps ...

enter image description here

Svedr
  • 589
  • 2
  • 6
  • 21
  • Try reloading with `smoothState.load(window.location.reload(true));` - the `true` forces non-cached reload. – Miro Feb 19 '17 at 18:46
  • @Miro this does a hard reload of the page and therefore negates the effect of smoothState. It is probably the cache, is there any other way to clear it ? – Svedr Feb 19 '17 at 18:52

1 Answers1

1

If it's caused by the cache you have a few options to try

Get around the cache

Append a random number (or the date) at the end of the request.

smoothState.load(window.location+'?'+Math.random());` 

You may want to change the ? to & if you're passing other variables.

Disable the cache in HTML

<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Expires" content="-1">

Disable the cache with the Header (in PHP)

<?php
  header("Cache-Control: no-cache, must-revalidate");
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
<!-- Here you'd start your page... -->
Miro
  • 8,402
  • 3
  • 34
  • 72