Home > Blockchain >  Jquery GET Function Returning Previous Result
Jquery GET Function Returning Previous Result

Time:02-21

I'm running into a rather strange problem. When the webpage loads, I automatically run a jQuery GET request on an internal PHP script that essentially prints a JSON output from a backend application.

jQuery

$.get(urls.validateSession, function(data){

     console.log(data); //For debugging
     var response = JSON.parse(data);

     //Do more things with returned data

});

PHP

<?php

    include 'include/backend.php';
    
    //Send POST request to backend application
    $response = sendPostRequest($urls->session->validate, json_encode($defaultRequest));

    /* sendPostRequest Function Definition */
    /*
    function sendPostRequest($url, $data){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);
        
        return $response;
    }
    */

    echo $response;

?>

This works as expected in my local xampp server, and will print a JSON output to the console similar to the following:

{
   "status":"SUCCESS",
   "time":"02-21-2022 02:14:03",
   etc...
}

Though when I run this same code on a remote apache server, instead of jQuery running the GET request each time the webpage loads and writing the received data to the console, jQuery will not run the GET request again and write the data received from the previous GET request to the console. Almost like the previous result of the GET request was cached and used again for the subsequent request.

I know jQuery is not running the GET request each time the page is reloaded because the "time" field in the JSON output always stays the same. As well, I do not see subsequent requests reaching the backend application. So jQuery is not bothering to make a new GET request to fetch new data, and is just using the previous result.

Though when I force refresh the webpage (Shift F5), this seems to force jQuery to run the GET request again, and the new expected JSON output with an updated timestamp is written to the console.

Any ideas about what could be happening here?

SOLUTION

Ajax GET requests have caching enabled by default. Adding the following code to the beginning of the JS files disables caching for all following ajax requests.

$.ajaxSetup({
   cache: false
});

CodePudding user response:

I think this previous question might help get you the desired result. The JQuery get() method's cache is, by default set to true.

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

And it sounds like your results are getting cached, that's why it doesn't bother to run the function each page load.

You can check out more ajaxSettings here

The snippet from @KevinB in the linked question:

$.get({
  url: url, 
  cache: false
}).then(function(rdata){
  console.log(rdata);
});
  • Related