Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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.
0
tmonteit
Asked:
tmonteit
  • 3
  • 2
  • 2
  • +3
2 Solutions
 
DireOrbAntCommented:
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
 
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ZvonkoSystems 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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now