Solved

Need Syncronous AJAX Call

Posted on 2006-11-15
11
323 Views
Last Modified: 2008-01-09
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
Comment
Question by:tmonteit
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 300 total points
ID: 17952965
Just pass false as the third parameter to YourAjaxObject.open() method like:
myReq.open("GET", "myUrl", false);
0
 

Author Comment

by:tmonteit
ID: 17953081
Can I get more code pls?   How do I setup and use the AJAX Object?
0
 

Author Comment

by:tmonteit
ID: 17953106
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 63

Accepted Solution

by:
Zvonko earned 200 total points
ID: 17953190
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
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17953449
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
 
LVL 6

Expert Comment

by:aescnt
ID: 17953864
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
 
LVL 15

Expert Comment

by:SnowFlake
ID: 17953993
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
 
LVL 18

Expert Comment

by:Morcalavin
ID: 17954688
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
 

Author Comment

by:tmonteit
ID: 17958346
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 17961141
:)
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17962551
tmonteit, that's exactly what Morcalavin's code does.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question