我正在使用以下 DOJO 功能进行 AJAX 调用。但不幸的是,特定请求的响应被缓存。即使我更改了 servlet 端的代码,它也不会反射(reflect)在我之前尝试过的情况下。我猜响应已被缓存。我正在使用 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"
}
});
}
最佳答案
实际上,您可以使用一个名为 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
请求来请求信息,因此这就是大多数浏览器缓存这些请求以提高性能的原因。所有其他类型的请求(POST
、PUT
...)通常不会被缓存。
关于javascript - 使用 DOJO 时会缓存 AJAX 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21251588/