为什么ajax调用缓存表单数据

Why does ajax call cache form data?

本文关键字:表单 数据 缓存 调用 ajax 为什么      更新时间:2023-09-26

下面的代码在第一次启动时运行良好,但如果我需要第二次发送(登录失败..),即使提交了新值,它也会重新发送原始表单数据。。。如何重置和重新加载数据。。

我已经尝试了很多东西,包括缓存:false(可能只适用于GET)和篡改url以避免浏览器缓存。。。到目前为止没有运气。请帮忙;-)

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()">

在JS-中

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

在PHP 中

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

同时使用两者可以避免缓存问题。

到目前为止,我提出的最好的解决方案是,如果用户/pw未通过身份验证,则执行location.reload();。在这种情况下,重新加载登录页面,Ajax调用将接收提交的新值,而不是原始/缓存的值。。。不过,如果有人有意见的话,仍然有兴趣了解代码的错误。。?

谢谢你的帮助!