sunwukung
sunwukung

Reputation: 2815

jQuery, Ajax and REDIRECT_QUERY_STRING

I've been attempting to refactor a fairly simple three or four page site to use a very lightweight MVC setup (probably overkill - but I thought I'd try it out for fun).

The site uses an .htaccess file:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#allow various filetypes
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [NC,L]

This allows me to grab the URL and run it through a router. However, when trying to issue an Ajax request (via jQuery) - there is a query string being appended to the URL (dumped from PHP):

['key'] =>'2?_=1282088000558'
//should be:
['key]=>'2'

checking the $_SERVER array, I see that the value is recorded as REDIRECT_QUERY_STRING

This doesn't seem to be a problem with Javascript disabled, can anyone offer any insight on this problem? Is there a way of preventing jQuery from inserting this value, or should I just remove it in JS?

Upvotes: 0

Views: 948

Answers (2)

Nick Craver
Nick Craver

Reputation: 630379

It's there to force the browser to not get the request from cache, it's really doing this, using the timestamp to get a fresh request back from the server...it's for the browser's benefit though:

url + _= + (new Date()).getTime(); 

You can see jQuery adding this here:

if ( s.cache === false && type === "GET" ) {
  var ts = jQuery.now();

  // try replacing _= if it is there
  var ret = s.url.replace(rts, "$1_=" + ts + "$2");

  // if nothing was replaced, add timestamp to the end
  s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
}

You can strip it off if you like by using cache: true in the $.ajax() options or by using $.ajaxSetup() like this:

$.ajaxSetup({ cache: true });

Upvotes: 2

The Mighty Rubber Duck
The Mighty Rubber Duck

Reputation: 4478

jQuery is adding a random component to the query string in order to avoid the request to hit the browser/server cache.

Upvotes: 1

Related Questions