Php 缓存的Ajax调用

Php 缓存的Ajax调用,php,jquery,json,ajax,caching,Php,Jquery,Json,Ajax,Caching,我正在构建的应用程序有问题。我已经阅读了许多关于类似问题的文章,并应用了这些文章中给出的建议。然而,问题依然存在,因此我写了这篇文章 的设置如下所示: $(document).ready(function () { var nocache = Math.random() * new Date().getTime() + Math.random(); $("#bookings").click(function () { $.a

我正在构建的应用程序有问题。我已经阅读了许多关于类似问题的文章,并应用了这些文章中给出的建议。然而,问题依然存在,因此我写了这篇文章

的设置如下所示:

$(document).ready(function () {
        var nocache = Math.random() * new Date().getTime() + Math.random();
            $("#bookings").click(function () {
                $.ajax({
                    url: 'step_one.php?cach='+nocache,
                    type: 'post',
                    cache: false,
                    success: function (data) {
                        $("#contentLeft").html(data);
                    }
                });
            });
        });
$("#viewprice").click(function () {
    var nocache = Math.random() * new Date().getTime() + Math.random();
    $.ajax({
        url: 'calculate_quote.php?cache=' + nocache,
        type: 'post',
        dataType: 'json',
        cache: false,
        data: $("#stepOneForm").serialize(),
        success: function (data) {
            console.log(data);
            $(".quote").append(data);
            $(".quote").show();
            document.getElementById("price").value = data;
        }
    });
});
  • 我有3个php文件:
    index.php
    step_one.php
    calculation.php
  • index.php
    ,我通过Ajax调用成功加载
    步骤_one.php
    ,如下所示:

    $(document).ready(function () {
            var nocache = Math.random() * new Date().getTime() + Math.random();
                $("#bookings").click(function () {
                    $.ajax({
                        url: 'step_one.php?cach='+nocache,
                        type: 'post',
                        cache: false,
                        success: function (data) {
                            $("#contentLeft").html(data);
                        }
                    });
                });
            });
    
    $("#viewprice").click(function () {
        var nocache = Math.random() * new Date().getTime() + Math.random();
        $.ajax({
            url: 'calculate_quote.php?cache=' + nocache,
            type: 'post',
            dataType: 'json',
            cache: false,
            data: $("#stepOneForm").serialize(),
            success: function (data) {
                console.log(data);
                $(".quote").append(data);
                $(".quote").show();
                document.getElementById("price").value = data;
            }
        });
    });
    
  • 注意:
    step_one.php
    是html表单。然后在
    step_one.php
    中,我在表单中输入数据,并通过另一个Ajax调用将表单数据发送到
    calculation.php
    ,如下所示:

    $(document).ready(function () {
            var nocache = Math.random() * new Date().getTime() + Math.random();
                $("#bookings").click(function () {
                    $.ajax({
                        url: 'step_one.php?cach='+nocache,
                        type: 'post',
                        cache: false,
                        success: function (data) {
                            $("#contentLeft").html(data);
                        }
                    });
                });
            });
    
    $("#viewprice").click(function () {
        var nocache = Math.random() * new Date().getTime() + Math.random();
        $.ajax({
            url: 'calculate_quote.php?cache=' + nocache,
            type: 'post',
            dataType: 'json',
            cache: false,
            data: $("#stepOneForm").serialize(),
            success: function (data) {
                console.log(data);
                $(".quote").append(data);
                $(".quote").show();
                document.getElementById("price").value = data;
            }
        });
    });
    
    calculation.php
    文件根据收到的数据计算价格,并将
    json
    返回到
    step_one.php
    。这就是我如何从
    calculation.php
    返回
    json

    header('Content-Type: application/json');
    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
    header('Expires: 0'); // Proxies.
    echo json_encode($data);
    
    注意:我第一次单击
    #viewprice
    按钮时,价格已正确并成功返回到
    步骤1.php
    。但是,当我在
    步骤1.php
    中输入新数据并重新单击
    #viewprice
    按钮时,
    计算.php
    不会返回任何内容。当我检查网络数据时,我看到
    calculation.php
    在那里被复制,并且只有第一个Ajax调用将在其响应中显示数据

    这个在xamp的本地机器上运行。 请你帮忙好吗?我做错了什么


    我认为上面的链接会有用>>>

    我发现了让我头疼的bug。这是一个逻辑错误

    背景

    出于安全考虑,我在表格中使用代币。因此,对于每个表单,我在页面加载时生成一个令牌,并将其存储在会话中。然后,当表单发送其数据(包括令牌)时,我首先检查接收到的令牌是否在会话中-如果找到令牌,我将使用接收值并使用它们计算
    $data
    ,然后将其传递给
    json\u encode
    函数。找到令牌后,我删除它


    因此,
    calculation.php
    没有被缓存,因为我的Ajax代码是正确的。相反,问题是当我重新发送表单数据进行重新计算时。在重发期间,会话中的令牌已被删除;因此,在会话中找不到我随表单数据发送的令牌。因此,数据不会被计算,也不会返回任何内容。

    应该:
    url:'step_-one.php?cach='+nocache,
    be:
    url:'step_-one.php?cache='+nocache,
    ?@RyanVincent查询字符串的名称并不重要,因此
    cach
    cache
    也会做同样的事情(不缓存请求)只要对应的值*是随机的,那么这就不应该是问题所在。我认为这与OP加载请求的方式和脚本有关,但我这里没有完整的案例(发布您认为相关的内容会遗漏很多其他开发人员需要帮助的相关代码。)@semu我会首先检查这些操作是否真的发生在JS中(例如,正确的事件被触发)您可以通过将
    console.log('got here')
    放在
    $('#viewprice')
    的clickhandler中来实现这一点-这样,每当您在console/devtools打开的情况下单击该按钮时,如果事件触发,您就会看到'got here',这样您就可以知道它是否在该处理程序中的某个位置。您可以对涉及的每个处理程序执行此操作,以更快地发现错误,并可能完全解决您的问题@Sidneylebrand,我遗漏的代码是用于表单设计的html和
    计算的php代码。用于计算价格和赋值的php
    是在将变量传递给
    json\u encode
    函数之前分配给
    $data
    变量。@Sidneylebrand,每次单击都会成功触发事件。我用谷歌Chrome开发工具检查了这一点