0

I have a form on which I do a POST to a webservice. Behind the submit button of the form I have this jquery code:

$("#msform").submit(function (e) {
    $.ajax({
        url:'https://example',
        type:'POST',
        data:formData1,
        crossDomain: true,
        dataType: 'json',
        jsonpCallback: 'callback',
        success: function(data) {
            console.log(data);
        }
    });
});

Now this code used to work, but when I changed my cache control it seemed not to work..

In my html I have this code for cache control:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

I added this cache control because I want the users to see new changes on the website whenever uploaded. In the past they needed to click CTRL+F5 to see the new changes or to use the new javascript.

In the past, when AJAX worked, I had this line: which I changed to:

Now when the user submits the form the post will not be received. But if the user goes back to the form page and submits the data again, then the post will be received.

Are there any ways to not save cache but continue doing AJAX Posts?

--edit

I removed the ajax post and substituted it to a php post

Nuri Ensing
  • 1,899
  • 1
  • 19
  • 42

2 Answers2

0

if you are referring to make sure they see the latest versions of code you upload, you should just add a version variable to the files you want the browser to reload. any time you update, you can just increment the version variable. for instance:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js?version=2.3"></script>

by changing the value of the version number "?version=2.3", the request will not get pulled from cache the next time. but it will until you change it again, so you will have the benefit of caching still which can greatly speed up your web application. the same can be done for css, html, or any other static content.

ChrisThompson
  • 1,998
  • 12
  • 17
0

Your dataType might also need dataType: 'jsonp'. But adding cache: false to you ajax options may work:

$.ajax({
    cache: false,
    url:'https://example',
    type:'POST',
    data:formData1,
    crossDomain: true,
    dataType: 'json',
    jsonpCallback: 'callback',
    success: function(data) {
        console.log(data);
    }
});

cache (default: true, false for dataType 'script' and 'jsonp')

If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

Joe
  • 80,724
  • 18
  • 127
  • 145