2011-12-06 47 views
2

我在加载图像时遇到问题。图像只在第一次加载,之后我得到相同的图像。只有第一次去photoHandler.ashx。 这里发生了什么?是jQuery缓存图像?怎么做总是加载正确的图像? 这是我的函数:是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 
    } 
}); 
} 

编辑: 如果我去的元素与萤火虫,我可以看到正确的“anUniqueIdentifier”。 MyHandler.ashx总是被调用。我与photoHandler.ashx有问题。我加了随机的,但它并没有为我工作:

var randomQS = "&Id=" + Math.round(new Date().getTime()/1000); 
//... 
$photo.fadeIn('slow'); 
}).attr('src', myObj.photoSrc + randomQS); 

更新:

我解决它,但问题是PhotoHandler.ashx,该控制器缓存图像,这种方式添加随机值网址不起作用。

谢谢。

+0

如果有什么,它是浏览器,而不是jQuery,它是缓存。 –

回答

3

您的浏览器缓存,你需要通过附加一个随机字符串,它打破了缓存的形象:

http://domain.com/myimage.jpg?random=12893128971844 

您可以使用类似JS数学FN:Math.random()

2

尝试添加一个时间戳到URL的末尾。将var theTime放入您的函数中,并将?和theTime附加到您的网址中。

var theTime = Math.round(new Date().getTime()/1000); 

var hUrl = "myHandler.ashx?"+theTime; 
0

添加时间戳到图像源:

...

var $photo = $("<img alt='' class='hidden' />").load(function(){ 
    $("#photo-container").append($photo); 
    $photo.fadeIn('slow'); 
}).attr('src', myObj.photoSrc + Math.round(new Date().getTime()/1000)); 

...