2014-01-21 47 views
0

我正在使用下面的DOJO功能进行AJAX调用。但不幸的是,缓存特定请求的缓存。即使我改变了servlet端的代码,它也不会反映在我以前尝试过的情况中。我想这个响应被缓存了。我正在使用Tomcat服务器。 任何人都可以帮忙吗?使用DOJO时缓存AJAX响应

<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

其实是有,你可以使用一个属性就是所谓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" 
       } 
      }); 
} 

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

+0

谢谢迪米特里。那个工作。并感谢您的额外信息。 – Rob

+0

+1用于解释'preventCache'的工作原理 – undefined

0

我解决了它。我在函数内部使用了'preventCache:true'。