2014-09-24 25 views
1

下面的代碼在第一次啓動時工作的很完美,但是如果我需要再次發送(登錄失敗..)它會重新發送原始表單數據,即使提交了新的值也是如此......如何重置並重新加載數據..爲什麼ajax調用緩存表單數據?

我已經嘗試了大量的東西,包括緩存:假(這大概只適用於GET)和搞亂網址,以避免瀏覽器緩存...迄今沒有運氣。請幫助;-)

function procLogin() { 

    $.ajax({ 
     type: 'POST', 
     url: 'http://www.lalala.com/lalala/auth.php?' + Math.random(), 
     crossDomain: true, 
     data: $("#loginForm").serialize(), 
     dataType: 'json', 
     async: false, 

     success: function(response) { 
      if (response.statusTFD == 'success') { 
       alert("Success"); 
      } else if (response.statusTFD == 'error') { 
       alert("Authentication Invalid. Please try again!"); 
      } else if (response.statusTFD.substring(0, 18) == 'Connection Failed:' || response.statusTFD.substring(0, 6) == 'Error:') { 
       alert(response.statusTFD); 
      } else { 
       alert("!¤#%¤!#" + response); 
      } 
     }, 
     error: function(error) { 
      //alert(response.success); 
      alert('Could not connect to the database' + error); 
     } 
    }); 
    return false; 
} 

上述被調用的onsubmit:

<form id="loginForm" method="POST" onsubmit="procLogin()"> 
+0

您是否使用IE進行測試?即使是IE的新版本也因緩存XHR請求而臭名昭着。 編輯 - 我錯過了你已經在嘗試Math.random()技巧,所以我必須進一步調查。 – 2014-09-24 17:52:09

+1

如何調用procLogin()?謝謝 – guest271314 2014-09-24 18:14:34

+0

不要使用'async:false' – Bergi 2014-09-24 18:21:49

回答

0

在JS -

$.ajaxSetup({ 
     cache: false, 
     headers: { 
      'Cache-Control': 'no-cache' 
     } 
    }); 

在PHP

header('cache-control: no-cache'); 

使用既避免緩存問題。

+0

嗨Raj,感謝您的輸入。我嘗試了您的建議,但將標題添加到我的php沒有區別 - 將它添加到AJAX返回錯誤... – tfddk 2014-09-24 19:45:01

0

我到目前爲止所提出的最佳解決方案是,如果用戶/密碼未經過身份驗證,則執行location.reload();。在這種情況下,登錄頁面被重新加載,並且Ajax調用將接收到提交的新值,而不是原始/緩存的值...仍然對學習代碼有問題感興趣,但如果任何人有輸入..?

感謝您的幫助!