使用DOJO时缓存AJAX响应

时间:2014-01-21 07:16:03

标签: javascript jquery ajax caching dojo

我正在使用以下DOJO功能进行AJAX调用。但不幸的是,缓存了特定请求的保留。即使我改变了servlet方面的代码,也没有反映出我之前尝试过的情况。我猜这个反应是缓存的。我正在使用Tomcat服务器。 任何人都可以帮忙吗?

<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"
                }
            });
}

2 个答案:

答案 0 :(得分:2)

实际上有一个可以使用的属性叫preventCache,它会为每个请求添加时间戳(因此永远不会使用缓存)如果你在true上设置它,你可以阅读更多关于它在reference guide

在你的情况下,它将是:

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"
                }
            });
}

小记:缓存不是特定于Dojo的,因为GET请求应该用于请求信息,这就是大多数浏览器缓存这些请求以提高性能的原因。所有其他类型的请求(POSTPUT,...)通常都不会被缓存。

答案 1 :(得分:0)

我解决了。我在函数中使用了'preventCache:true'。