Click event not fireing when page is inactive for some time
Ok, this is my problem :). I have couple of [WebMethods] in my code behind and using jquery ajax to get data from server. And then it happens :). After some time while page is inactive when i try to click on button which should have send request to server it simply does nothing for about half of minute and only then event is fired and i get response from server.
my javascript looks something like this:
addToCart.click(function () {
AddOrRemoveItemToCart($(this));
});
function AddOrRemoveItemToCart(control)
{
var itemId = contol.attr("id");
$('document').ready(function () {
$.ajax({
type: "POST",
url: "Home.aspx/AddOrRemoveItemToCart",
data: "{itemId:" + itemId + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
SucessAddItemToCart(data.d);
}
},
error: function (textStatus) {
alert(textStatus);
}
});
});
}
function SucessAddItemToCart(data)
{
//DO SOMETHING WITH DATA
}
And my server side cod开发者_Go百科e look something like:
[WebMethod]
public static List<CartItem> AddOrRemoveItemToCart(string itemId)
{
List<CartItem> items = new List<CartItem>();
List<CartItem>temp = new List<CartItem>();
bool check = false;
if(HttpContext.Current.Session["items"]!=null)
{
items = (List<CartItem>)HttpContext.Current.Session["items"];
foreach(CartItem c in items)
{
if(c.Id != itemId)
temp.Add(c);
else
check = true;
}
if(!check)
temp.Add(new CartItem{Id = itemId});
}
HttpContext.Current.Session["items"]=temp;
return temp;
}
Normally I would have said your Session expired. But since the event fires after half a minute, it has to be something else.
Try to debug with Firebug and check if AddOrRemoveItemToCart gets called immediately. You can also see the traffic between the browser and the server.
Your event should fire immediately but as Remy said your Session has probably expired, and it takes some time to re-establish the session object and process the request.
精彩评论