0

My JavaScript function is as bellow :

function getActivityDiscussed(str,id)
{
            var xmlhttp;    
            if (str=="")
            {
              document.getElementById("list_bottom").innerHTML="";
              return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                document.getElementById("list_bottom"+id).innerHTML=xmlhttp.responseText;
              }
            }
            xmlhttp.open("GET","<?php echo base_url($client_url.'communications/getActivityDiscussed'); ?>/"+str,true);
            xmlhttp.setRequestHeader("Cache-Control", "no-cache");
            xmlhttp.setRequestHeader("Pragma", "no-cache");
            xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
            xmlhttp.send();
 }

This ajax call is not working in IE-9.

I have cleared the cache by using following code :

xmlhttp.setRequestHeader("Cache-Control", "no-cache");
xmlhttp.setRequestHeader("Pragma", "no-cache");
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

But I have to use these code for each ajax request.

Is there any way to disable the cache globally?

Thanks in Advance

Bikram Pahi
  • 1,050
  • 1
  • 12
  • 31

2 Answers2

0

You might need to add some random number or string for every request which will solve this issue.

check this link.. will be helpful. Prevent browser caching of jQuery AJAX call result

Community
  • 1
  • 1
Mahendra
  • 303
  • 1
  • 7
  • I don't think your solution is best, Because if an user send multiple ajax request then it will fill the browser cache. I have the solution, but I want to set some global config so that there is no repetition of code. – Bikram Pahi Jan 05 '15 at 07:23
  • have you tried using cache: false in jquery for that?

    you must have included which methods you have tried and these are not desired.
    – Mahendra Jan 05 '15 at 07:27
  • Have you gone through my code properly? Before answer read the question thoroughly, If any doubt ask question again I will clarify. – Bikram Pahi Jan 05 '15 at 07:30
  • 1
    You can change the call from a GET to a POST. POST by design will not get cached. But if you must use GET then adding a current date time stamp in the query string is the best solution – Van Jan 05 '15 at 08:28
0

According to my knowledge there is no such mechanism available in JavaScript. You have to repeat it regularly otherwise you can use these code in a function and call the function where you want.

function clearBrowserCacheForAjax()
{
   xmlhttp.setRequestHeader("Cache-Control", "no-cache");
   xmlhttp.setRequestHeader("Pragma", "no-cache");
   xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
}

In each function you can call the above function.

function getActivityDiscussed(str,id)
{
            var xmlhttp;    
            if (str=="")
            {
              document.getElementById("list_bottom").innerHTML="";
              return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                document.getElementById("list_bottom"+id).innerHTML=xmlhttp.responseText;
              }
            }
            xmlhttp.open("GET","<?php echo base_url($client_url.'communications/getActivityDiscussed'); ?>/"+str,true);
            clearBrowserCacheForAjax();
            xmlhttp.send();
 }