AJAX response is being cached
Once again I am posting the question similar to previous one but this time my problem is different. As said the code is as follows:
$.ajax({
type:"POST",
url: 'sample.php',
data:"data="+data,
success: function(server_response) {
$('.functions').ajaxComplete(function(event, request){
if(server_response == 0)
开发者_运维问答 {
var message = 'An email with instructions to reset password has been sent to your email address. Please check it.';
}
else if(server_response == 1)
{
var message = 'Email address was not found in our database. Please enter a valid email address';
}
else
{
var message = 'Some problem occured while sending you an email. Please try again.';
}
alert(message);
});
The problem is whenever I pass an ajax request the server response received is correct but if above code is executed for multiple times without page refresh multiple dialog boxes are recieved in a stack order. Like if my first response is 0 then the message "An email with instructions to reset ...." is recieved. However if I send the request again without page refresh and this my if my page response is 1 then 2 dialogs are generated with message "Email address was not found in our database." on top and the previous one below. Such stack continues to generate until I refresh the page. I have tried all things such as setting cache to false and all stuff but all in vain. please help.
Alternative pass cache: false
in the jquery ajax options like as follows:
$.ajax({
type:"POST",
cache: false,
url: 'sample.php',
data:"data="+data,
success: function(server_response) {
$('.functions').ajaxComplete(function(event, request){
if(server_response == 0)
{
var message = 'An email with instructions to reset password has been sent to your email address. Please check it.';
}
else if(server_response == 1)
{
var message = 'Email address was not found in our database. Please enter a valid email address';
}
else
{
var message = 'Some problem occured while sending you an email. Please try again.';
}
alert(message);
});
2 ways for you:
$.ajaxSetup({
cache: false,
});
OR
$.ajax({
type:"POST",
cache: false,
url: 'sample.php',
data:"data="+data,
success: function(server_response) {
$('.functions').ajaxComplete(function(event, request){
if(server_response == 0)
{
var message = 'An email with instructions to reset password has been sent to your email address. Please check it.';
}
else if(server_response == 1)
{
var message = 'Email address was not found in our database. Please enter a valid email address';
}
else
{
var message = 'Some problem occured while sending you an email. Please try again.';
}
alert(message);
});
I was having this exact issue on a site being viewed through an iOS device. It was somehow steamrolling the
cache: false
setting. So I fixed it with a tried and true hack:
var d = new Date();
$.ajax({
url : "blah.php?"+d.getTime(),
cache: false,
...
I hope this helps anyone else facing the same issue.
POST requests are never cached.
Try $.post instead
$.post('sample.php', {"data="+data}, function(server_response) {
if(server_response == 0)
{
var message = 'An email with instructions to reset password has been sent to your email address. Please check it.';
}
else if(server_response == 1)
{
var message = 'Email address was not found in our database. Please enter a valid email address';
}
else
{
var message = 'Some problem occured while sending you an email. Please try again.';
}
});
精彩评论