Skip to content

Commit

Permalink
Fixed #1999 by replacing the 'no-cache' parameter if it is there inst…
Browse files Browse the repository at this point in the history
…ead of just appending.
  • Loading branch information
davids549 committed Dec 4, 2007
1 parent 701b072 commit aee221d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/ajax.js
Expand Up @@ -197,8 +197,13 @@ jQuery.extend({
if ( s.dataType == "script" && s.cache == null ) if ( s.dataType == "script" && s.cache == null )
s.cache = false; s.cache = false;


if ( s.cache === false && s.type.toLowerCase() == "get" ) if ( s.cache === false && s.type.toLowerCase() == "get" ) {
s.url += (s.url.match(/\?/) ? "&" : "?") + "_=" + (new Date()).getTime(); var ts = (new Date()).getTime();
// try replacing _= if it is there
var ret = s.url.replace(/(\?|&)_=.*(&|$)/, "$1_=" + ts + "$2");
// if nothing was replaced, add timestamp to the end
s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
}


// If data is available, append data to url for get requests // If data is available, append data to url for get requests
if ( s.data && s.type.toLowerCase() == "get" ) { if ( s.data && s.type.toLowerCase() == "get" ) {
Expand Down
32 changes: 31 additions & 1 deletion test/unit/ajax.js
Expand Up @@ -226,7 +226,7 @@ test("pass-through request object", function() {
var success = function() { var success = function() {
// Re-enabled because a bug was found in the unit test that probably caused the problem // Re-enabled because a bug was found in the unit test that probably caused the problem
if(++count == 5) if(++count == 5)
start(); start();
}; };


ok( $.get(url(target), success), "get" ); ok( $.get(url(target), success), "get" );
Expand All @@ -236,6 +236,36 @@ test("pass-through request object", function() {
ok( $.ajax({url: url(target), success: success}), "generic" ); ok( $.ajax({url: url(target), success: success}), "generic" );
}); });


test("ajax cache", function () {
expect(18);
stop();

var count = 0;

$("#firstp").bind("ajaxSuccess", function (e, xml, s) {
var re = /_=(.*?)(&|$)/g;
var oldOne = null;
for (var i = 0; i < 6; i++) {
var ret = re.exec(s.url);
if (!ret) {
break;
}
oldOne = ret[1];
}
equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
if(++count == 6)
start();
});

ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
});

test("global ajaxSettings", function() { test("global ajaxSettings", function() {
expect(3); expect(3);


Expand Down

0 comments on commit aee221d

Please sign in to comment.