最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent cache in every Dojo xhr request on page - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Jan 25, 2013 at 20:58 Panu HaaramoPanu Haaramo 2,93222 silver badges43 bronze badges 8
  • Can you not modify your request URL prior to sending the xhr request? Or in the cases where you want to prevent caching you can add preventCache: true to your dojo xhr request. – pjdanfor Commented Jan 25, 2013 at 21:16
  • @pjdanfor Not without modifying Dojo source. Sorry I should have included this info in my question (see also below my ment for the answer). The question has been modified now. – Panu Haaramo Commented Jan 25, 2013 at 21:20
  • You could subclass JsonRestStore and implement the method that issues the xhr request to include your additional functionality. – pjdanfor Commented Jan 25, 2013 at 21:22
  • Good idea. But the Dojo/JavaScript code is generated by Domino server which makes it difficult or impossible to do things like that in this case. – Panu Haaramo Commented Jan 25, 2013 at 21:29
  • That's unfortunate - canceling requests every time you have to prevent cache for a particular endpoint and resending it really isn't a good solution though. – pjdanfor Commented Jan 25, 2013 at 22:06
 |  Show 3 more ments

2 Answers 2

Reset to default 2

Both dojo/io/script and dojo/xhr have a preventCache parameter that does exactly what you are trying to do. So instead of trying to intercept, can you just add preventCache: true to the request arguments?

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

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

The answer es 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/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.

发布评论

评论列表(0)

  1. 暂无评论