Prevent browser from using cache with ajax

1

Good morning, I have the following code which verifies me through ajax to a file called ultima_cuenta.php that makes a query and returns the last id of a table called accounts, this code is executed once the password is opened. page and it works perfectly, the problem is that if I need it again, it always brings me the same account even though it is not the last one, I read it is cache issues, I search for several parts and tried to put the ajax parameter. cache = false; but it does not work

for it to work I must refresh the explorer with shift + F5 in the case of chromem any idea to prevent that? I must be doing something wrong in the parameter that I put

thank you very much !!

I have the function

function ultima_cuenta(){
var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var respuesta=this.responseText;
      var respuesta_entero = parseInt(respuesta);
      $('#label_ultima_cuenta').text(respuesta_entero+1);
    }
  };
  xhttp.open("GET", "../PHP/ultima_cuenta.php", true);
  ajax.cache=false; // intenté esto
  xhttp.send();
}
  

ultima_cuenta.php

<?php

require 'conectar.php';

$consulta = "SELECT * FROM cuentas ORDER BY id DESC LIMIT 1" or die('Error. '.mysql_error());

$resultado_consulta = mysqli_query($conexion, $consulta);

$fila = mysqli_fetch_array($resultado_consulta, MYSQLI_ASSOC);

echo $fila["id"];
?>
    
asked by Gabriel Uribe Gomez 26.03.2018 в 06:18
source

1 answer

1

Change from GET to POST .

  • Requests GET always go through the cache. It is assumed that, if you request a resource, it will always be the same, and it is not necessary to search for it to the server.

  • Requests POST never go through the cache. You are asking the server to perform an action, and the result does not have to be the same in successive invocations.

    xhttp.open( "POST", "../PHP/ultima_cuenta.php", true );
    
answered by 26.03.2018 в 06:29