Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need Syncronous AJAX Call

Posted on 2006-11-15
11
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 1200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Accepted Solution

by:
Zvonko earned 800 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

721 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