Search code examples
c#jqueryajaxkendo-asp.net-mvc

how to handle session timeout when ajax call from kendo combobox read action


How to handle session timeout when using kendo combo-box?

Below is my html code for kendo combobox

@(Html.Kendo().ComboBoxFor(model => model.PropertyName)
                              .AutoBind(true)
                              .Suggest(true)
                              .DataTextField("Text")
                              .DataValueField("Value")
                              .DataSource(source =>
                              {
                                  source.Read(read =>
                                  {
                                      read.Action("ActionName", "ControllerName");
                                  })
                                  .ServerFiltering(true);
                              })
                              .Animation(false)
                              .Filter("contains")
                              .HighlightFirst(false)                              
                )

When read.Action called at that time in controller session timeout occurred and I have written custom attribute to check for session expire and return 403 status code using below code

if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                    {
                        filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Forbidden");
                        return;
                    }

And this returned status handled in layout.cshtml page using below code and reload the page to go to login page

$.ajaxSetup({
        error: function (x, e) {
            if (x.status == 403) {
                window.location.reload();
            }
        }
    });

It works for all ajax request but not work when ajax request from kendo combobox. please, help me to handle it while kendo combobox ajax request.


Solution

  • I have resolved this session timeout issue by putting complete event in ajaxsetup. when ajax request occurred from kendo at that time when response get with 403 then it will not consider as error it called complete event not called error event so after putting complete event page redirected to login page

    $.ajaxSetup({
            error: function (x, e) {
                if (x.status == 403) {
    
                    window.location.reload();
                }
            },
            complete: function (x, e) {
                if (x.status == 403) {
    
                    window.location.reload();
                }
            }
        });  
    

    It works as I expected.