views:

42

answers:

2

I am using json to get the list of user information to show on the top of the page using jquery in asp.net (C#) application.

i am using jquery timer plugin for the json call to check for the user information frequently(for every 15 minute when the user is in online).

When the json request is in progress, and when i navigate to the next page, the json request gets cancelled/stops, which can be clearly seen using firebug in mozilla.

How to keep the json call going till it gets its response?

A: 

Keeping the AJAX request around for a page that is no longer open doesn't make sense, as there's nowhere for the response handler to display results.

Create a header that's included on every page. When the page is loaded, the header should display the current values. Start a new AJAX request when the new page is opened.

outis
i'll have a <div> to the show the content in my master page(not on a specific page).
Prasad
@Prasad - you can't think of a master page as a separate page from a client's point of view. The master page and its contents are delivered in one hit as if it's one response. Navigating generates and entire new page - nothing stays on the client's side.
Damovisa
All the variables within a script are scoped to a specific page as viewed in the browser ("global" variables are actually properties of `window`). It's the URI that determines whether two pages are the same, not the script that generates a page. Change the URI (excepting the fragment ID) and you get a new page, losing access to all old variables.
outis
+1  A: 

Based on the copious comments I've seen, I'd suggest one of the following:

Option 1:

  • In the master page, call the function that requests the information in a json call straight away. This means that whenever the page changes, it requests the new information.
  • Keep your 15min interval just in case the user sits on the same page for 15min.

Option 2:

Use AJAX partial updates to update only the main contents of the page. If the browser isn't requesting entire new pages, the code in the masterpage never actually gets refreshed. This sounds like it's how you expected the site to behave, but it's a lot of rework to actually get this happening.

Explanation:

I think the major misunderstanding here is what happens when a user navigates to a new page.

You may have heard before that the web is stateless. What that means is that when you're on a web page and you navigate to a new page, the entire contents of the page you're on are discarded, and the server hands you a brand new page - top to bottom. None of the html in the original page is kept.

Using master pages doesn't change anything. Master pages are really just used as wrappers on the server side so that it knows to include the header and footer in every page you send back. You're still going to be sending back a completely new page.

What this means for your example is that cancelling the json request is the only sensible thing for the browser to do. You've made an AJAX request for some information, and while it's waiting for a response, the user has navigated to a new page. The browser knows that the server will deliver a brand spanking new page with completely new html, so it throws out the current page. The div that was going to be updated literally doesn't exist any more.

Damovisa