weixin_33726313 2017-03-13 01:32 采纳率: 0%
浏览 38

超时后重定向视图

I have a view Index, called from Index Action inside EnqueteController, which has all my javascript code in it. This view RenderPartial a form, _EnqueteForm which has all my form inputs. In my javascript section (in Index) I have the following:

@section Scripts{

<script type="text/javascript">

var IDLE_TIMEOUT = 10; //seconds
var _idleSecondsCounter = 0;
document.onclick = function () {
    _idleSecondsCounter = 0;
};
document.onmousemove = function () {
    _idleSecondsCounter = 0;
};
document.onkeypress = function () {
    _idleSecondsCounter = 0;
};
window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime() {
    _idleSecondsCounter++;
    if (oPanel)
        oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
    if (_idleSecondsCounter >= IDLE_TIMEOUT) {
        $.ajax({
            cache: false,
            url: '/Enquete/Inactive',
            type: 'POST',
            dataType: "json",
            data: $('form').serialize(),
        });
    }
}

}

After 10 seconds of inactivity, it's calling Inactive action from Enquete Controller.

This action looks like this:

    [HttpPost]
    public ActionResult Inactive([System.Web.Http.FromBody] InfoFormulaireEnqueteModele m)
    {
        int userId = this.UserId();

        LibraryEnquete.EnregistrerFormulaire(m, userId);
        TransitionEtatPrecedent(m.HevEvenementID, userId);
        return View("Logout");
    }

My action is called correctly after 10s and it's calling LibraryEnquete.EnrigistrerFormulaire and also TransitionEtatPrecedent. But my problems are:

1) It wont change to the 'Logout' view

2) For some reason, my Inactive method is called from other page?! After one call, I stop the application and once I restart it, from the main page (which is not the EnqueteController Index page), it's calling the Inactive method like 20 times until I stop the application

  • 写回答

1条回答 默认 最新

  • weixin_33717117 2017-03-13 02:09
    关注

    Ajax calls do not handle redirect responses (http status code 3xx).

    By returning the view to an ajax call, you are just getting the view rendered as string in the ajax success event handler.

    In order for the browser to render the logout view, you can just proceed to submit the form without using ajax:

    if (_idleSecondsCounter >= IDLE_TIMEOUT) {
        $('form').submit();
    }
    

    If the form's action it's not poiting to /Enquete/Inactive, you can change the action before submiting the form:

    if (_idleSecondsCounter >= IDLE_TIMEOUT) {
        $('form').prop('action', '/Enquete/Inactive');
        $('form').submit();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)