k.anantharengan
k.anantharengan

Reputation: 1

How to handle session timeout during json calls in asp.net mvc3 web application?

In our project we make a lot of json callbacks for various operations. I will give you an example of one such ajax call below

$.ajax({
    url: '../Controller1/Method1',
    type: "POST",
    data: "{'Id': '" + Id "'}",
    dataType: "html",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        $('#div').html(result);
    }, error: function (s) {
        ShowAlert(s.responseText);
    }
});

Now when the session is timed out and if we are making a json ajax callback,then it is not redirecting to the login page properly , so how to handle session timeout during json ajax callback and if the session is expired and a ajax callback is made it should redirect me to the login page.

How to achieve this?. Any help(code samples,useful links) would be very much appreciated.

Upvotes: 0

Views: 1905

Answers (2)

Chirag Arvadia
Chirag Arvadia

Reputation: 1200

When ajax call is made and session is timeout the return with specific error message, now if responce is come with specific error message then simply do a window.location =

Upvotes: 2

nanobar
nanobar

Reputation: 66355

is 12030 a http response? why not return or throw (depending on if you're in a web-api or normal controller):

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

then in the ajax call:

$.ajax({
  //.........
  statusCode: {
      408: function () {
          console.log('Call timed out');
      }
  }
});

perhaps

Upvotes: 0

Related Questions