简体   繁体   English

阻止页面上每个Dojo xhr请求中的缓存

[英]Prevent cache in every Dojo xhr request on page

I'm able to intercept Dojo 1.6.1 xhr requests using IO Pipeline Topics as described here: 我能够使用IO管道主题来拦截Dojo 1.6.1 xhr请求,如下所述:

Dojo - intercepting XHR calls Dojo-拦截XHR呼叫

I would like to add a time parameter to the URL (fe &time=12345 ) to prevent cache in certain (or all) xhr GET requests originating from dojox.data.JsonRestStore (details of what I'm trying to achieve are here ). 我想在URL(fe &time=12345 )中添加一个时间参数,以防止在源自dojox.data.JsonRestStore某些(或全部)xhr GET请求中进行缓存(有关详细信息,请参见此处 )。 My code looks like this: 我的代码如下所示:

dojo.subscribe("/dojo/io/send", function(deferred) {

    if (deferred.ioArgs.url.indexOf("restService1") > -1) {
        deferred.cancel();
        deferred.ioArgs.url += '&time=12345' // test value at this point
        dojo.xhrGet(deferrred.ioArgs);
    }
});

Basically I'm trying to cancel the request, add a string to URL and then make the request with the modified URL. 基本上,我试图取消请求,向URL添加一个字符串,然后使用修改后的URL发出请求。

This does not work at all: the request with modified URL does not end up to the server and I'm getting a lot of these errors to browser console: 这根本不起作用:带有修改的URL的请求不会最终到达服务器,并且在浏览器控制台中收到很多这些错误:

Chrome错误

The errors occur in line 14 of dojo.js. 错误发生在dojo.js的第14行中。 The Chrome tab crashes eventually after these errors. 这些错误最终导致Chrome标签页崩溃。

I also tried just modifying deferred.ioArgs.url and doing nothing else but that has no effect. 我还尝试仅修改deferred.ioArgs.url ,什么也不做,但这没有任何效果。

Both dojo/io/script and dojo/xhr have a preventCache parameter that does exactly what you are trying to do. dojo/io/scriptdojo/xhr都有一个preventCache参数,该参数确实可以完成您要尝试的操作。 So instead of trying to intercept, can you just add preventCache: true to the request arguments? 因此,除了尝试拦截之外,您还可以在请求参数中添加preventCache: true吗?

http://dojotoolkit.org/reference-guide/1.6/dojo/io/script.html#dojo-io-script http://dojotoolkit.org/reference-guide/1.6/dojo/io/script.html#dojo-io-script

http://dojotoolkit.org/reference-guide/1.6/dojo/xhrGet.html#dojo-xhrget http://dojotoolkit.org/reference-guide/1.6/dojo/xhrGet.html#dojo-xhrget

The answer comes once again from Sven Hasselbach : 答案再次来自Sven Hasselbach

/**
 * Cache Prevention for Dojo xhr requests
 *
 * Adds no-cache header and enables dojo's preventCache feature
 * for every dojo xhr call. This prevents the caching of partial
 * refreshs.
 *
 * @author Sven Hasselbach
 * @version 0.3
 *
 **/
dojo.addOnLoad(
    function(){
        if( !dojo._xhr )
        dojo._xhr = dojo.xhr;

        dojo.xhr = function(){        
            try{
                var args = arguments[1];   
                args["preventCache"] = true;
                args["headers"] = { "cache-control": "no-cache" };
                arguments[1] = args;
          }catch(e){}

          dojo._xhr( arguments[0], arguments[1], arguments[2] );
        }
    }
)

http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache-prevention-for-dojo-xhr-requests http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache-prevention-for-dojo-xhr-requests

Tried it out and it does exactly what I was looking for by adding &dojo.preventCache=1359366392301 parameter to the xhr URLs. 尝试了一下,并通过向xhr URL添加&dojo.preventCache=1359366392301参数来实现我想要的功能。 And it seems to add a cache-control header too. 而且似乎也添加了一个缓存控制标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM