关于IE和edge浏览器中get请求缓存的坑。

主要是在一些使用到cookie的ajax场景中。

比如:Angularjs的$http({"url":url,"method":"GET",withCredentials":true}).success(function(){})

get请求无法得到正确数据的时候,先查看控制台。

如果该请求 from cache 或 来自缓存

你会发现该get请求的请求头为空。

这时就会向服务器发送空请求,导致服务器得不到你的cookie,从而无法根据cookie返回所需信息。

 

解决方法如下:

1.在链接后面加随机的索引字符串,比如时间戳什么的。写过验证码的都知道这个坑。

以angular为例:

// 时间戳
$http({
    url: myConstant.sqlUrl + "/uid" + "?time=" + (+new Date()),
    method: "GET",
    withCredentials: true,
    // 允许携带Cookie
    headers: {
        "If-Modified-Since": 0
    }
    // 避免get请求缓存
}).success(function (data) {
    $scope.userInfo = data.data;
});
// 随机数
$http({
    url: myConstant.sqlUrl + "/uid" + "?time=" + Math.random(),
    method: "GET",
    withCredentials: true,
    // 允许携带Cookie
    headers: {
        "If-Modified-Since": 0
    }
    // 避免get请求缓存
}).success(function (data) {
    $scope.userInfo = data.data;
});

 

2.直接在header里写上禁止缓存:{"If-Modified-Since":0}

以angular为例:

$http({
	url: myConstant.sqlUrl + "/uid",
	method: "GET",
	withCredentials: true,
	// 允许携带Cookie
	headers: {
		"If-Modified-Since": 0
	}
	// 避免get请求缓存
}).success(function (data) {
	$scope.userInfo = data.data;
});

3.使用php的header函数

header('Cache-Control:no-cache, must-revalidate');

  

转载于:https://www.cnblogs.com/Totooria-Hyperion/p/5635706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值