在页面上的每个Dojo xhr请求中防止缓存 [英] Prevent cache in every Dojo xhr request on page

查看:868
本文介绍了在页面上的每个Dojo xhr请求中防止缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 IO管道主题拦截Dojo 1.6.1 xhr请求,如下所述:





尝试它,它正好与我正在寻找的是添加& dojo.preventCache = 1359366392301 参数到xhr U的RL。而且似乎也添加了一个缓存控制头。


I'm able to intercept Dojo 1.6.1 xhr requests using IO Pipeline Topics as described here:

Dojo - intercepting XHR calls

I would like to add a time parameter to the URL (f.e. &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). 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.

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:

The errors occur in line 14 of dojo.js. The Chrome tab crashes eventually after these errors.

I also tried just modifying deferred.ioArgs.url and doing nothing else but that has no effect.

解决方案

The answer comes once again from 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

Tried it out and it does exactly what I was looking for by adding &dojo.preventCache=1359366392301 parameter to the xhr URLs. And it seems to add a cache-control header too.

这篇关于在页面上的每个Dojo xhr请求中防止缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆