Rob
Rob

Reputation: 451

AJAX response is cached when using DOJO

I am using the below DOJO functionality for AJAX call. But unfortunately the respose for a particular request is cached. And even if I change the code in servlet side, its is not reflected for the cases where I have tried earlier. I guess the reponse is cached. I am using Tomcat server. Can any one please help?

<script type="text/javascript">
function showMonth(text) { // 
  dojo.xhrGet({ 
    // The following URL must match that used to test the server.
    url: "ajaxServlet?s="+text, 
    handleAs: "text",
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs) { //
                    dojo.byId("response").innerHTML = response + "Hereeee"; // 
                    return response; // 
                },

    // The ERROR function will be called in an error case.
    error : function(response, ioArgs) { // 
                    console.error("HTTP status code: ", ioArgs.xhr.status); //
                    dojo.byId("response").innerHTML = 'Loading the ressource from the server did not work'; //  
                    return response; // 
                },

                // Here you put the parameters to the server side program 
                // We send two hard-coded parameters
                content : {
                    name : "lars",
                    url : "testing"
                }
            });
}

Upvotes: 0

Views: 577

Answers (2)

Rob
Rob

Reputation: 451

I resolved it. I used 'preventCache: true' inside the function.

Upvotes: 0

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44685

There is actually a property that you can use that's called preventCache that will add a timestamp to each request (so cache is never used) if you set it on true, you can read more about it at the reference guide.

In your case it would be:

function showMonth(text) { // 
  dojo.xhrGet({ 
    // The following URL must match that used to test the server.
    url: "ajaxServlet?s="+text, 
    handleAs: "text",
    preventCache: true,
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs) { //
                    dojo.byId("response").innerHTML = response + "Hereeee"; // 
                    return response; // 
                },

    // The ERROR function will be called in an error case.
    error : function(response, ioArgs) { // 
                    console.error("HTTP status code: ", ioArgs.xhr.status); //
                    dojo.byId("response").innerHTML = 'Loading the ressource from the server did not work'; //  
                    return response; // 
                },

                // Here you put the parameters to the server side program 
                // We send two hard-coded parameters
                content : {
                    name : "lars",
                    url : "testing"
                }
            });
}

Small note: The caching isn't Dojo specific, it's because GET request should be used to request information, so that's why most browsers cache these requests to improve performance. All other type of requests (POST, PUT, ...) are usually not cached.

Upvotes: 2

Related Questions