views:

207

answers:

2

Hi there!

I am a beginner using ajax and I always thought that it is completely asynchronous. But I discovered that a call can be interrupted by a page reload or a page change (like clicking on a hyperlink). I was under the impression that when an ajax call is started, it is carried out no matter what the browser does afterwards. Is that wrong?

Now to the specific problem I am having: think of an online test where users answer questions (by typing into textboxes). When a textbox loses focus, an ajax call is triggered which persists the value of the textbox to a DB. That works well when changing between textboxes. However, I also have a submit button which triggeres a post action to another page (it is the submit button). When I enter something into a textbox and click on the button afterwards, the call is not carried out. Moreover, when I type into a textbox, click somewhere else (also triggering the call) and swiftly click on the submit button, the call is also not made. Is that expected behaviour?

The reason I am using ajax in the first place is to persist the values so when something unforseeable happens, like a browser crash, the already typed in text is already saved.

Is my way of thinking wrong? How would you go about solving this problem?

Thank you for your time!

+1  A: 

Unless you do something special with persistent local storage, all javascript and ajax calls are blown away when a new page is loaded over the current page. Also when a submit is done on a form.

To save things intra-page, save the data asap. Eg, perhaps save on key-up, perhaps periodically with a timer, not just on lose-focus.

Re submitting the page: change the on-click behavior to first store, then to go to a new page.

All of the effects that you are seeing are normal.

Also, be sure to test on both slow (ie 6 or 7) and fast browsers (chrome)

Larry K
+2  A: 

AJAX is asynchronous.

When you send an AJAX request the javascript engine sends it off and sets up a handler for the response.

However, if you send an AJAX request to the server and then navigate away from the page before it is received, nothing will happen. Why? Because with each page load the entire Javascript environment is tore down and reinitialized, it has no idea what happened on the last page.

For your problem I would intercept the form submit action and do whatever you need to do with the data, and then submit the form.

Edit: In response to your comment. You are correct. If the ajax request is sent, and you're not depending on it's return value, then it should not matter.

I'd suggest debugging your problem with Firebug to see if the AJAX call is really being sent properly, and to confirm your server is properly processing it.

hobodave
But in this example I don't care about the return value. The server doesn't send anything back anyway.My understanding of (true) asynchrony is that as soon as the the call is "dispatched", it is on it's way to the server and can't be interrupted by the browser/user anymore. But when I read your answer, it seems like JavaScript is somehow holding back the call in order to be able to interrupt it. But for how long and why?
Maximilian Csuk