正在缓存AJAX响应

时间:2011-08-04 10:48:35

标签: jquery ajax

我再次发布类似于前一个问题,但这次我的问题不同了。如上所述,代码如下:

$.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);

    });

问题在于,每当我传递ajax请求时,收到的服务器响应都是正确的,但如果上面的代码多次执行而没有页面刷新,则会按堆栈顺序收到多个对话框。就像我的第一个响应是0然后收到消息“带有重置指令的电子邮件....”。但是,如果我再次发送请求而没有页面刷新,那么如果我的页面响应为1,则生成2个对话框,并显示消息“我们的数据库中未找到电子邮件地址”。在顶部和前一个下面。这样的堆栈继续生成,直到我刷新页面。我已经尝试过所有的事情,例如将缓存设置为false以及所有东西,但都是徒劳的。请帮忙。

4 个答案:

答案 0 :(得分:2)

jquery ajax选项中的替代传递cache: false,如下所示:

$.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);

    });

答案 1 :(得分:2)

2种方式:

$.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);

    });

答案 2 :(得分:1)

我在通过iOS设备查看的网站上遇到了这个问题。它以某种方式推动了

cache: false

设置。所以我用一个久经考验的黑客修复了它:

var d = new Date();
$.ajax({
    url : "blah.php?"+d.getTime(),
    cache: false,
    ...

我希望这有助于其他任何面临同样问题的人。

答案 3 :(得分:0)

POST请求永远不会被缓存。

尝试$ .post

$.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.';                                    
      }
});