Solved

Need Syncronous AJAX Call

Posted on 2006-11-15
11
320 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now