首页 > 解决方案 > 提示时 Ajax 不执行新请求

问题描述

我仍在学习 Ajax,每次我想使用 ajax 更改页面内容时,我都会使用以下脚本:

function load_page(id)
{
    $.ajaxSetup({ cache: false });

    var toId = "";

    $('#ajax-intro-body').show();

    if (id == "dashboard")
        toId = "../src/pages/partials/" + id + ".php";
    else
        toId = "../src/pages/" + id + ".php";

    $.ajax({
        url:toId,
        method:"POST",
        cache: false,
        headers: { "cache-control": "no-cache" },
        data:{},
        success:function(data)
        {
         if (data == "session_expired"){
             location.replace("../src/pages/login/")
         }else{
              // Show output data
              $('#ajax-content').html(data);
              // Set refresh address id
              $('.refresh-content').attr('id',id);
              // Fade out the loading bar body
              $('#ajax-intro-body').fadeOut(100);
              // Remove animation from sync
              $('#refresh-icon').removeClass("fa-spin");
         }
        }
    });
}

所以我认为代码很容易理解。现在我有一个“项目页面”,你也可以在其中删除它,这个页面正在使用上面的脚本加载,我在 php 中得到了以下代码:

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        if (isset($_GET['id'])){ 

        $("#dialog-3").dialog({
        autoOpen: false,//remove this and the click to aut open
        bgiframe: true,
        width: 340,
        modal: true,
        resizable: false,
        buttons: {
                "Delete":function(){
                        $(this).dialog("close");

                        $.ajax({
                                type: "POST",
                                url: "../src/requests/deleteproject.php",
                                data: {
                                    id: <?php echo $projectid; ?>,
                                    flag: true,
                                },
                                success: function (data) {
                                    load_page('projects/listprojects');
                                }
                        });

                },
                Cancel: function() {
                        $(this).dialog("close");
                }
        }
});
}
}

因此,如果我想删除它,您可以看到我正在尝试在项目页面中执行和 ajax 请求。使用浏览器网络选项卡,我可以看到,当我调用项目页面时,实际上 GET 参数 id 正在更改为正确的数字,但实际上它并没有更新 ID id: <?php echo $projectid; ?>,。我读过我需要在ajax请求中将缓存设置为false,但它似乎不起作用,所以我无法更新Id。

你们有什么想法吗?谢谢你!

标签: phpajax

解决方案


推荐阅读