【问题标题】:append an html tag right after $.ajax loads to another page在 $.ajax 加载到另一个页面后立即附加一个 html 标记
【发布时间】:2015-02-21 02:30:18
【问题描述】:
$('#form').on('submit', function(e){


        $.ajax({
            type: $(this).attr('method'),
            url:  $(this).attr('action'),
            data: $(this).serialize(),
            dataType: 'json'

        }).done(function(data){ 

                    window.location.href = "manage_item.php";
                    $('body').append('<div>Item Added Successfully!</div>');


           });
});

当 ajax 将数据添加到我的库存时,我正尝试加载到另一个页面并显示成功警报。我怎么解决这个问题?使用 $.ajax 链接页面的最佳做法是什么,或者除了使用 $.ajax 之外还有其他替代方法吗?

【问题讨论】:

    标签: jquery ajax form-submit onsubmit ajaxform


    【解决方案1】:

    最好的方法是在你的 ajax 成功后传递一个 get 变量和 url,如下所示:

    .done(function(data){ 
    
                        window.location.href = "manage_item.php?success=1";       
               });
    

    在你的 manage_item.php 中做这样的事情:

    if (isset($_GET['success'] && $_GET['success'] == 1)){
    echo "<div>Item Added Successfully!</div>"
    }
    

    【讨论】:

    • 可以不使用$_GET吗?因为我不想显示额外的链接。我可以在另一个 ajax 的 .done() 中执行类似 ajax 的操作吗?
    • 一种厚颜无耻的方式是使用 document.referrer;在 manage_item.php 中了解以前的 url。如果以前的 url === ajax 来自的页面则显示成功,但这是非常不鼓励的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2014-04-25
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多