为什么cache:false在jQuery AJAX中不起作用

时间:2016-10-17 10:44:19

标签: javascript jquery caching

我在我的例子中使用AJAX。在这里我使用了cache: false属性。 它总是触发请求(我在网络选项卡中检查)。为什么请求没有缓存?这是我的代码

http://codepen.io/anon/pen/WGgKvd?editors=1010

$(function(){
    $('#btn').click(function(){
        alert('----')

        $.ajax({
            url: "https://api.github.com/events",
            data: {
                id: 123
            },
            cache: false,
            type: "GET",
            dataType : "json",
        }).done(function(json) {
            $("<h1>").text(json.title).appendTo("body");
            $("<div class=\"content\">").html(json.html).appendTo("body");
        }).fail(function(xhr, status, errorThrown) {
            alert("Sorry, there was a problem!");
            console.log("Error: " + errorThrown);
            console.log("Status: " + status);
            console.dir(xhr);
        }).always(function(xhr, status) {
            alert("The request is complete!");
        });
    })
})

1 个答案:

答案 0 :(得分:1)

根据jQuery API,如果缓存设置为false,它将强制浏览器不缓存请求的页面。

我猜您应该将其更改为true