tmonteit
asked on
Need Syncronous AJAX Call
How do I write a syncronous AJAX call that puts the response into a JavaScript var?
Needs to be a javascript function
var x = callServer(a,b) {
//append a&b to URL and call servlet.
return response_text;
}
Please help!!!, I've gotten lots of advice, but no one can seem to show me how to make it work correctly.
Needs to be a javascript function
var x = callServer(a,b) {
//append a&b to URL and call servlet.
return response_text;
}
Please help!!!, I've gotten lots of advice, but no one can seem to show me how to make it work correctly.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please make this basic. All these AJAX examples keep adding extra functions that are supposed to be called when the response comes.
I just want to RETURN the request from my own callServer method.
I just want to RETURN the request from my own callServer method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To make it work in non-IE PC, change:
var myReq = new ActiveXObject("Msxml2.XMLH TTP");
To:
var myReq;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
myReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
myReq = new ActiveXObject("Microsoft.X MLHTTP");
}
var myReq = new ActiveXObject("Msxml2.XMLH
To:
var myReq;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
myReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
myReq = new ActiveXObject("Microsoft.X
}
Just to clarify for those who are going to post, tmonteit has posted this question before and what he wants is a call that works like so:
function GetValue() {
value = /* get this via some ajax method */;
return value;
}
Typical AJAX calls will have to define an `onResponse` callback function which will be fired when a response is received. What he wants though, is for the GetValue() to wait for the response and return the value from the responseText (instead of defining some sort of callback).
(I personally don't think this is really possible...)
function GetValue() {
value = /* get this via some ajax method */;
return value;
}
Typical AJAX calls will have to define an `onResponse` callback function which will be fired when a response is received. What he wants though, is for the GetValue() to wait for the response and return the value from the responseText (instead of defining some sort of callback).
(I personally don't think this is really possible...)
Just wanted to note that generally speaking -its not such a good idea to do what you are asking.
the only advantage this has is in more readable code.
technically you would be better of if
instead of
function foo(){
//part1 code
//sync ajax call
//part2 code()
}
you would use
function foo(){
//part1 code
//async ajax call with foo_2 as callback
}
function foo_2 {
//part2 code
}
this was:
1) the gui won't freeze for the duration of the sync call
2) you can implement timeouts
3) your app is less likely to stuck if the call is just lost.
SnowFlake
the only advantage this has is in more readable code.
technically you would be better of if
instead of
function foo(){
//part1 code
//sync ajax call
//part2 code()
}
you would use
function foo(){
//part1 code
//async ajax call with foo_2 as callback
}
function foo_2 {
//part2 code
}
this was:
1) the gui won't freeze for the duration of the sync call
2) you can implement timeouts
3) your app is less likely to stuck if the call is just lost.
SnowFlake
I already answered this question, twice, but you keep opening new questions(I think the last count was three). A synchronous call that will wait for a return was demonstrated in the last response here:
https://www.experts-exchange.com/questions/22061931/Prototype-Library-Question.html
https://www.experts-exchange.com/questions/22061931/Prototype-Library-Question.html
ASKER
Morocalvin, your responses were helpful, but I needed a 'RETURN VALUE' from my primary function.
Zvonko was the first to show me that.
his example was simple, direct and complete.
Zvonko was the first to show me that.
his example was simple, direct and complete.
:)
tmonteit, that's exactly what Morcalavin's code does.
ASKER