【问题标题】:how to redirect user on success of ajax call如何在ajax调用成功时重定向用户
【发布时间】:2014-04-02 11:27:54
【问题描述】:

我正在使用twitter bootstrap,我有一个生日Modal,它要求用户输入他们的date of birth,验证并将数据插入数据库工作正常,但我需要将用户重定向到另一个页面(仪表板。 php) 成功后,下面是我正在使用的 ajax 代码

$('#dobform').submit(function (e) {
    "use strict";
    e.preventDefault();
    $('#dobsubmit').button('loading');
    var post = $('#dobform').serialize();
    var action = $('#dobform').attr('action');
    $("#message").slideUp(350, function () {
        $('#message').hide();
        $.post(action, post, function (data) {
            $('#message').html(data);
            document.getElementById('message').innerHTML = data;
            $('#message').slideDown('slow');
            if (data.match('success') !== null) {
                $('#dobform').slideUp('slow');
                $('#dobsubmit').button('complete');
                $('#dobsubmit').click(function (eb) {
                    eb.preventDefault();
                    $('#dob-form').modal('hide');
                });
            }
            else {
                $('#dobsubmit').button('reset');
            }
        });
    });
});

我尝试在else 中使用以下重定向代码,但无论是否出错,它都会重定向用户

window.location = "dashboard.php"

我尝试过使用里面的重定向

if (data.match('success') !== null) {

}

但这也不起作用,我非常感谢任何帮助。

【问题讨论】:

  • @Jai 我在服务器端尝试过,但它没有重定向
  • 重定向服务器端对于 AJAX 请求来说是无稽之谈。 data 实际上 包含什么(将其记录到控制台以查看)?还有你为什么用match来检查呢?
  • @CBroe 除了match 还有什么可以使用的作为回应,我想在数据库中插入数据并将用户重定向到另一个页面
  • match 用于正则表达式。如果您被问到明确的问题,请回答
  • 如果您想检查数据是否为空,请使用:if(data !== null) 或者如果您想检查数据是否“成功”:if(data === 'success')

标签: php jquery ajax twitter-bootstrap redirect


【解决方案1】:

终于搞定了,只好拿出match

$('#dobform').submit(function (e) {
    "use strict";
    e.preventDefault();
    $('#dobsubmit').button('loading');
    var post = $('#dobform').serialize();
    var action = $('#dobform').attr('action');
    $("#message").slideUp(350, function () {
        $('#message').hide();
        $.post(action, post, function (data) {
            $('#message').html(data);
            document.getElementById('message').innerHTML = data;
            $('#message').slideDown('slow');
            if (data == "Thankyou") {
                $('#dobform').slideUp('slow');
                $('#dobsubmit').button('complete');
                window.location = "dashboard.php";
                $('#dobsubmit').click(function (eb) {
                    eb.preventDefault();
                    $('#dob-form').modal('hide');
                });
            }
            else {

                $('#dobsubmit').button('reset');
            }
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2015-04-11
    • 2014-10-25
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多