我在加载图像的函数方面遇到问题。图像仅在第一次加载,之后我得到相同的图像。只有第一次才会转到 photoHandler.ashx。 这里会发生什么? jquery 是否缓存图像?如何做到始终加载正确的图像? 这是我的功能:
function getObjectFromServer()
{
var hUrl = "myHandler.ashx";
var data = {};
var data.queryStringKey = "theKey";
$.post(hUrl, data, function(response){
if(response.length>0)
{
var myObj = jQuery.parseJSON(response);
var $photo = $("<img alt='' class='hidden' />").load(function(){
$("#photo-container").append($photo);
$photo.fadeIn('slow');
}).attr('src', myObj.photoSrc);
//myObj.photoSrc contains: photoHandler.ashx?photoId=anUniqueIdentifier
}
});
}
编辑: 如果我使用 firebug 转到该元素,我可以看到正确的“anUniqueIdentifier”。 MyHandler.ashx 始终被调用。我在使用 photoHandler.ashx 时遇到问题。我添加了随机但它对我不起作用:
var randomQS = "&Id=" + Math.round(new Date().getTime() / 1000);
//...
$photo.fadeIn('slow');
}).attr('src', myObj.photoSrc + randomQS);
更新:
我解决了,问题是PhotoHandler.ashx,这个 Controller 正在缓存图像,这种方式向url添加随机值不起作用。
谢谢。
最佳答案
您的浏览器正在缓存您需要通过向其附加随机字符串来打破缓存的图像:
http://domain.com/myimage.jpg?random=12893128971844
你可以使用类似 JS math fn 的东西:Math.random()
关于jquery - JQuery 是否缓存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8406368/