像许多其他人一样,我也遇到了 IE 和缓存方面的问题。我有一个拍卖网站,当用户点击出价时,触发此代码:
function bid(id){
var end_dateUP=0;
var tupdate=jq("#tupdate"+id).val();
if (tupdate=="lvl1"){
end_dateUP=20;
}else if (tupdate=="lvl2"){
end_dateUP=15;
}else if (tupdate=="lvl3"){
end_dateUP=10;
}else{
end_dateUP=0;
}
var url = "http://localhost/bid/comet/update-auction.php"; // the script where you handle the form input.
var user_id=<?php echo json_encode($user_id);?>; //here i'm getting id from SESSION
var user_name=<?php echo json_encode($user_name); ?>; //here i'm getting user name from SESSION
jq.ajax({
type: "POST",
async: false,
url: url,
data: {"auct_id" : id, "user_id" : user_id, "username" : user_name, "end_date" : end_dateUP}, // serializes the form's elements.
cache:false,
success: function(data)
{
setTimeout('waitForMsg()',100);
jq("#tupdate"+id).val("");
jq('#bid-container'+id).animate({ backgroundColor: "#659ae0" }, 50);
jq('#bid-container'+id).animate({ backgroundColor: "#FFF" }, 500);
},
error: function(xhr, errorString, exception) {
alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|");
}
});
}
在 PHP (update-auction.php) 中,我获取此发布的 ajax 数据并更新我的数据库:
$auction_id=$_POST['auct_id'];
$user_id=$_POST['user_id'];
$usr=$_POST['username'];
$enddate=$_POST['end_date'];
//Database update
此代码在 Firefox 或 Chrome 中运行良好。
所以问题是,当我第一次点击出价时,它有效,但是当我进入第二页时(下面的代码):
function pageClick(page){
var url = "http://localhost/bid/auctions-ajax"; // the script where you handle the form input.
jq.ajaxSetup({ cache: false }); //this line before $.ajax!!!
jq.ajax({
type: "POST",
url: url,
data: {"page" : page},
async:false, //to sem dodal za časovni zamik
cache: false,
success: function(data)
{
jq("#a_loader").hide();
jq("#show-category").html(data); // show response from the php script.
},
error: function(xhr, errorString, exception) {
alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|");
}
});
}
(onClick 会触发 ajax 调用并显示第二页),然后此函数 bid(id)
停止工作。我搜索过类似 cache:false,
添加 new Date().time();
来以 JSON 格式发布和发布数据的解决方案,但没有成功。
(此外,当我尝试以 JSON 格式发布数据时,我遇到了一些语法错误:意外的标记 < 和解析错误等等)。我只是想用这个工作代码找出最简单的解决方案......有什么想法吗?
最佳答案
这解决了我的问题:
$.ajax(url,
{
cache: false,
success: function(){
//do something
}
}
);
“原因是,有些浏览器只要get请求中的请求相同,就会缓存结果。”
示例:
function showBootstrapModalPopup(url, containerId, modalId)
{
$.ajax(url,
{
cache: false,
success: function (data) {
//do something
$('#' + containerId).html(data);
$('#' + modalId).modal('show');
}
}
);
//$.ajax({
// url: url,
// success: function (data) {
// $('#' + containerId).html(data);
// $('#' + modalId).modal('show');
// },
// cache: false
//});
}
关于internet-explorer - 在 Internet Explorer 中第二次调用后 jQuery ajax 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938737/