javascript - 使用 DOJO 时会缓存 AJAX 响应

标签 javascript jquery ajax caching dojo

我正在使用以下 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 请求来请求信息,因此这就是大多数浏览器缓存这些请求以提高性能的原因。所有其他类型的请求(POSTPUT...)通常不会被缓存。

关于javascript - 使用 DOJO 时会缓存 AJAX 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21251588/

相关文章:

javascript - 如何阻止用户重复单击 jQuery AJAX 调用?

javascript - ajax成功延迟不起作用

javascript - 如何使用流约束基于另一种类型的类型?

javascript - jQuery 删除除第一个和最后一个数字之外的所有数字

javascript - Sequelize 6.13.4 版的 requestTimeout 的替代方案是什么?

javascript - 基于动态生成的文本和特定图案的颜色 li 元素

javascript - 过滤 HTML 表格,结果应位于 HTML 表格中

javascript - Chrome 如何获取 gist json 文件?这不是禁止的吗?

javascript - 如何加速 PHP 数学运算?

javascript - 合并这两个 if...else 语句