/    Sign up×
Community /Pin to ProfileBookmark

ajax synchronous request, page refresh and the error

Hi,
I have been using asynchronous requests for a long time. so the response was processed in a callback function. I thought of not using async so i made synchronous requests. The reason is that i dont have to have two more lines for checking the status and the onreadystatechange….

my synchronous requests would be like this…

[CODE]var jax = createAjax(); // this function returns the XMLHTTPRequest object
jax.open(‘POST’,url,false); //here false is to do sync request.
jax.setRequestHeader(‘Content-Type: application/x-www-form-urlencoded’);
jax.send(parameters);
// once the above line is executed the process waits for the response and when it gets the response form the server then the next line is executed.
alert(jax.responseText); // here i process the response text.[/CODE]

so from the code you can understand that there is no need of a callback function and if conditions to check ready state… …

So there is no problem in the above code. The problem araises here…

if i press the F5 (refresh) key or do a page refresh when the process is waiting for the response i get an (NS ERROR – firefox ( i have not yet checked that in IE browsers) (javascript error) though the process completes successfully. why?

Do we have to check whether the page is navigating away while in synchronous operation and abort the request?

or what could be the reason for the error. This will not happen in async requests because that is also the reason for async…

so i give a sync request and before the response is received if i navigate away or reload the page i get the error.

???

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Dec 12.2009 — If your reason for using synchronous request is to get rid of two lines of code, you really need to rethink that.

Eric
Copy linkTweet thisAlerts:
@jayapalchandranauthorDec 12.2009 — async is not a problem i can always juggle that. I want to know the reason for the browser showing the error.
×

Success!

Help @jayapalchandran spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...