开发者

Why this JQuery Ajax won't be cached

开发者 https://www.devze.com 2023-04-08 19:57 出处:网络
Here\'s my script. I want the result to be cached but it keeps sending requested requests if my mouse enters it again.

Here's my script. I want the result to be cached but it keeps sending requested requests if my mouse enters it again.

$('.poplist li').mouseenter(function(){
    t=$(this).parent().attr("rel");
    i=$(this).attr("rel");
    $.ajax({
        url:"/j/place",
        data:{"t":t,"tid":i},
        cache:true,
        success:function(data){
            data= jQuery.parseJSON(da开发者_StackOverflow社区ta);
            s=$('.left[rel='+t+']');
            s.find('.intro').html(data["intro"]);
            s.find('.shop-avat img').attr("src","http://img.douban.com/u/plao/"+data['img']);
        }
    })
})


jQuery.ajax's cache option does nothing when set to true, and caching is entirely left up to the browser. If it is set to false, however, a cache-busting timestamp is appended to the request. That's all.

Update: To see that this is so, take a look at the the source of jQuery.ajax, version 1.6.4, line 676, here: https://github.com/jquery/jquery/blob/1.6.4/src/ajax.js#L676


A simple solution is,

You may save the result (data) in a variable and you may use an if function before your ajax request. On mouse enter, if variable have no data stored, trigger ajax request, else, return false.

0

精彩评论

暂无评论...
验证码 换一张
取 消