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.
tmonteitAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ZvonkoConnect With a Mentor Systems architectCommented:
As DireOrbAnt  already stated:
Just pass false as the third parameter to YourAjaxObject.open() method like:
myReq.open("GET", "myUrl", false);

My ammendment:

var x = callServer(a,b);

function callServer(aPar,bPar) {
  var myReq = new ActiveXObject("Msxml2.XMLHTTP");
  //append a&b to URL and call servlet.
  myReq.open("GET", "myServletUrl?a="+aPar+"&b="+bPar, false);
  myReq.send(null);
  return myReq.responseText;
}

0
 
DireOrbAntConnect With a Mentor Commented:
Just pass false as the third parameter to YourAjaxObject.open() method like:
myReq.open("GET", "myUrl", false);
0
 
tmonteitAuthor Commented:
Can I get more code pls?   How do I setup and use the AJAX Object?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
tmonteitAuthor Commented:
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.
0
 
DireOrbAntCommented:
To make it work in non-IE PC, change:
var myReq = new ActiveXObject("Msxml2.XMLHTTP");
To:
var myReq;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    myReq = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    myReq = new ActiveXObject("Microsoft.XMLHTTP");
}
0
 
aescntCommented:
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...)
0
 
SnowFlakeCommented:
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
0
 
MorcalavinCommented:
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:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_22061931.html
0
 
tmonteitAuthor Commented:
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.
0
 
ZvonkoSystems architectCommented:
:)
0
 
aescntCommented:
tmonteit, that's exactly what Morcalavin's code does.
0
All Courses

From novice to tech pro — start learning today.