0

I have an application where I am making a call to a webmethod from Js.

 $.ajax({
    type: "POST",
    url: "mypage.aspx/MyWebMethod",
    ....
    ....
    });

In my C# webmethod I have set

[WebMethod(EnableSession = true)]

The problem is very strange and I donot even know if it is a Jquery or C# or server setting problem.

Problem: When I run the webpage and start using the application, everything working fine. However, if I just keep the webpage open (for something like 5 min) and donot do anything with it..and then try to use the application, the ajax call fails.

Can someone please let me know what the problem could be?

madth3
  • 7,275
  • 12
  • 50
  • 74
pessi
  • 681
  • 1
  • 10
  • 26
  • 1
    Ok, try this: Start the server (F5), and do your ajax call. Then wait 5 minutes, or however long it took for it to stop working, and then try opening a page on your server, and see if it loads. Then, without refreshing your first page, do the ajax call, and see if it works. – Jeff Aug 26 '12 at 12:01
  • 1
    you're using a webmethode with EnableSession=true and your session might been expired after 5 min for that reason your ajax call fails consider making the expiry date of your session more longer – Sora Aug 26 '12 at 12:34
  • @Sora ... how do I make the session last longer..? – pessi Aug 26 '12 at 12:50
  • change the expiry date in your web.config file do some search in google :) – Sora Aug 26 '12 at 12:51
  • The problem was with the session variables which are being wiped off every 10 min or so..I couldnot set the time to higher value even through global.asax...therefore I stored the session variable which was not very sensitive in a hidden variable and used it.. – pessi Aug 26 '12 at 15:03

2 Answers2

1

I strongly recommend you change this:

[WebMethod(EnableSession = true)]

To this:

HttpContext.Current.Session

That way you don't need to use (EnableSession = true) I tested my own and there is no problem

Good luck!

Jake1164
  • 12,291
  • 6
  • 47
  • 64
Gonza Oviedo
  • 1,312
  • 15
  • 20
0

Its likely that the ASP Session is expiring, and you are getting an HTTP 401 or similar.

You can either handle the in your ajax error{} callback, or alternatively consider various workarounds for keeping the session alive in the background.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285