【问题标题】:Redirect in jQuery AJAX using setInterval() (Polling)使用 setInterval() 在 jQuery AJAX 中重定向(轮询)
【发布时间】:2014-06-26 09:20:22
【问题描述】:

我有一个使用 setInterval() 的简单队列系统,我使用 jquery ajax 检查后端的队列。当满足某些条件时,我想重定向到该人可以继续该过程的页面。

在 Firefox 上,我得到了预期的重定向,但在 IE 和 Chrome 上却没有。 IE 简单地给出“无法显示此页面”错误,Chrome 给出“此网页存在重定向循环”错误。

有什么想法吗?

这是我的代码:

$(document).ready(function () {
    StartTheTimer();

    function StartTheTimer(e) {
        var sec = parseInt($("#RefreshSeconds").val()) + 1;

        var timer = setInterval(function () {
            $('#spanQueueCountdown').text(--sec);

            if (sec == 1) {
                $('#spanSeconds').text('second');
            }
            else {
                $('#spanSeconds').text('seconds');
            }

            if (sec == 0) {

                clearInterval(timer);

                $.ajax({
                    type: "POST",
                    url: "/Purchase/CheckQueue",
                    data: {
                        applicationSessionId: $("#ApplicationSessionId").val(),
                        eventId: $("#EventId").val(),
                        ordernumber: $("#OrderNumber").val()
                    },
                    cache: false,
                    dataType: "json",
                    success: function (result, textStatus, jqXHR) {
                        if (result.success && jqXHR.status == 200) {
                            if (!result.redirect) {
                                $("#spanQueueNumber").text(result.positionInQueue);

                                StartTheTimer();
                            }
                            else {
                                window.location.replace(result.redirecturl);
                            }
                        }
                        else {
                            console.log('Checking the queue failed.')
                            window.location.href = "/";
                        }
                    },
                    error: function () {
                        console.log('Error in checking queue.')
                        window.location.href = "/";
                    }

                });
            }

            if ($('#spanQueueNumber').text() == 0) {
                clearInterval(timer);
                $('#spanQueueCountdown').text(0);
            }
        }, 1000);
    }
});

【问题讨论】:

    标签: jquery ajax asp.net-mvc redirect


    【解决方案1】:

    StartTheTimer 方法后缺少右括号}

    【讨论】:

    • 用我目前的脚本更新了脚本,仍然没有被重定向
    【解决方案2】:

    我找到了解决问题的方法,但并不理想。

    我在页面中添加了一个额外的 div,其中包含一个链接,一旦用户排在队列的前面,就需要点击该链接。我能够 - 从 ajax 成功方法中 - 隐藏解释在队列中的过程的部分,然后显示带有要遵循的链接的 div。

    但是,请求的行为是让页面在用户到达队列的最前面时自动重定向。所以这是一种妥协。

    如果有人可以向我解释为什么我试图完成的事情是不可能的,请这样做。否则,如果还有其他人知道我做错了什么,请也这样做。我很乐意满足客户的期望。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多