如何在asp.net mvc3中的ajax调用上处理会话超时错误

时间:2012-01-19 13:55:50

标签: jquery ajax asp.net-mvc-3

  

可能重复:
  Handling session timeout in ajax calls

我想在ajax调用上处理超时错误,我将会话超时设置为1分钟,如..

<authentication mode="Forms">
    forms loginUrl="~/User/Login"  timeout="1" />

/认证&GT;

我对每个ajax请求的调用就像.. $.ajax({

        url: '../Document/getData?did=' + docid2,

        beforeSend: function () {

            $("#DialogData").empty().html('<p class="Loadimg"></p>');
        },
        success: function (result) {

            $("#DialogData").load('../Document/getData?did=' + docid2);
        },

        error: function (req, status, xhr) {
            alert(xhr);
            //for Timeout Error
            if (status == "timeout") {

                $("#DialogData").empty().html('<p>Plese Try Again</p><');
            }


            if (status == "error") {
                //for Page Not Found Invalid URL
                if (xhr == "Not Found") {

                }
                //for server error which is from controller
                if (xhr == "Internal Server Error") {

                }
            } //if error
        } //error
    }); //Ajax

but on timeout i get internal server error...i want to handle timeout error differently than other error so what should i do..where i can differntiate this error.because now if session timeout it gives internal server error or if any other server error occure it gives same internal server error as xhr value..when session timeout it display that login page which is redirect from controller is in div where i am calling $("#DIVID").load(..)....i want to redirect as soon as session timout on load to entire login page... how can i do...thanks in advanced..

my code from controller side for redirection is..

        url: '../Document/getData?did=' + docid2,

        beforeSend: function () {

            $("#DialogData").empty().html('<p class="Loadimg"></p>');
        },
        success: function (result) {

            $("#DialogData").load('../Document/getData?did=' + docid2);
        },

        error: function (req, status, xhr) {
            alert(xhr);
            //for Timeout Error
            if (status == "timeout") {

                $("#DialogData").empty().html('<p>Plese Try Again</p><');
            }


            if (status == "error") {
                //for Page Not Found Invalid URL
                if (xhr == "Not Found") {

                }
                //for server error which is from controller
                if (xhr == "Internal Server Error") {

                }
            } //if error
        } //error
    }); //Ajax

1 个答案:

答案 0 :(得分:0)

我建议您following article,它说明了如果表单身份验证票证到期,您可以如何配置ASP.NET以发送401状态代码。这允许您在AJAX请求中拦截此状态代码并采取相应的操作。

但是如果你得到一个500错误代码,这意味着你的服务器端代码有问题,你必须首先解决。