所以,我的网站上有很多ajax调用,我想用随机字符串附加所有网址。
那么,怎么做到这一点?
(询问和回答所以它可用)。
答案 0 :(得分:4)
您可以使用$.ajaxPrefilter
修改任何$.ajax
选项。
$.ajaxPrefilter(function(options) {
options.url += getRandomString(); // defining getRandomString() left as an exercise for the reader
});
如果您只是想阻止缓存,请使用jQuery的内置cache: false
选项:
$.ajaxPrefilter(function(options) {
options.cache = false;
});
答案 1 :(得分:0)
您可以使用此代码:
function makeid(count) { //Makes a unique string.
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var text = "";
for (var x = 0; x < count; x++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
(function($) { //Extend the existing jquery ajax call.
var _ajax = $.ajax;
$.extend({
ajax: function(o) {
if(o.url){
split='?';
if(o.url.indexOf('?') > 0) split='&'; //Do we already have a bunch of parameters in the url? if so, use & instead of ?
o.url+=split + 'string=' + makeid(10); //append to the string
}
return _ajax.call(this,o);
}
});
})(jQuery);