ajax rount-trip in javascript if/else statement

P

pbd22

can somebody tell me how i can use an ajax call in a
javascript if/else statement that only goes to the next
else statement after the ajax round-trip returns a true/false
result? code appreciated.

thanks!
 
T

Thomas 'PointedEars' Lahn

pbd22 said:
can somebody tell me how i can use an ajax call in a
javascript if/else statement that only goes to the next
else statement after the ajax round-trip returns a true/false
result?

1. You can't.
2. Make the request-response handling synchronous (not recommended).
code appreciated.

You mistyped "doing your homework" only slightly.



PointedEars
 
P

pbd22

so how do you make an xmlhttp request part of a
conditional code block without making the call synchronous?
 
H

Hamish Campbell

how i can use an ajax call in a javascript
You mistyped "doing your homework" only slightly.

Yep, homework question number 1: "What does the first A in AJAX stand
for?"
 
H

Hamish Campbell

so how do you make an xmlhttp request part of a
conditional code block without making the call synchronous?

You don't, you use the readystatechange event to call a function when
a response is received.
 
H

Hamish Campbell

That's interesting.  I never thought about that before -- that AJAX was
an acronym.  So why, then, is it still possible to make an AJAX call
synchronous?  It sounds like that would be an oxymoron.

What you do with the *xmlhttp* object is entirely up to you. Calling
it AJAX doesn't make it so.
 
R

Richard Cornford

Fair enough. Since it is the same structure and process,
maybe we should call that SyJAX :) ?

Maybe we should take the point that synchronous XML HTTP requests are
such a seriously bad idea that there is no need for inventing a name
for them, as they wouldn't be being used.

Richard.
 
G

Gregor Kofler

sheldonlg meinte:
That's interesting. I never thought about that before -- that AJAX was
an acronym. So why, then, is it still possible to make an AJAX call
synchronous? It sounds like that would be an oxymoron.

You don't even need XML with AJAX.

Gregor
 
P

pbd22

You don't, you use the readystatechange event to call a function when
a response is received.

I understand how an xmlhttp request/response works, but I am having
problem with the following (pseudocode):

if (condition) { //do something
}else if (condition) { // use xmlhttp to get a result of some sort.
{ else { // do something }

The problem with the above is that the code does not wait for the
round-trip xmlhttp response. It hits the else statement that fires the
request, considers its work done, and moves on. I understand that this
is caused by the "A" in AJAX but, if i have to get a result from the
server to satisfy a condition in a JavaScript code block, how might I
otherwise go about it (that is not "SynJax")?

Thanks.
 
M

Matthias Reuter

pbd22 said:
I understand how an xmlhttp request/response works, but I am having
problem with the following (pseudocode):

if (condition) { //do something
}else if (condition) { // use xmlhttp to get a result of some sort.
{ else { // do something }

The problem with the above is that the code does not wait for the
round-trip xmlhttp response. It hits the else statement that fires the
request, considers its work done, and moves on. I understand that this
is caused by the "A" in AJAX but, if i have to get a result from the
server to satisfy a condition in a JavaScript code block, how might I
otherwise go about it (that is not "SynJax")?

Well, you have to start using callbacks.

if (condition) {
doSomething();
}
else if (condition2) {
doHttpRequest(doSomething2);
}
else {
doSomething3();
}

with functions doSomething, doSomething2 and doSomething3.

Example:

if (emailAddress) {
sendMail(emailAddress);
}
else {
loadAddress();
}

function loadAddress () {
// ... get xmlhttprequest

r.onreadystatechange = function () {
emailAddress = r.responseText;
sendEmail(emailAddress);
};

// ... send request
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top