How to return value from Ajax Function – Use synchronous request

Have you ever tried to return values from the Ajax function ? Well it might be sometime the cases when you might have to return the value got from the server to another function rather than updating the component.

Let’s try to return the values from the traditional Ajax function.



function getVal(param)
  var strURL=”findValues.php?val=”+param;
  var req = getXMLHTTP();
  if (req)
    req.onreadystatechange = function()
      if (req.readyState == 4)
        return req.responseText;
    }”GET”, strURL, true);

And now look at the following statement ,


What does the alert statement displays? Is it the value returned from the server?? Not at all, it alert “undefined”. Why so?? What happens here is you are trying to get the synchronous request but the request runs asynchronously.So to return the value, we need to make the request synchronous so that we can return the value from the Ajax function.

So what is the difference between synchronous request and asynchronous request of Ajax?? In the above example which is asynchronous request, the callback function is called in the background leaving the other activity of your program to flow no matter weather the result is obtained from the server or not. But in the case of synchronous request, JavaScript stop processing your program until a result has been obtained from the server.

Now let’s look at the synchronous way of request of the above function

function getVal(param)
  var strURL=”findValues.php?val=”+param;
  var req = getXMLHTTP();”GET”, strURL, false); //third parameter is set to false here
  return req.responseText;

If you look at the open method, the third parameter is false which means the the request should be synchronous, if set to true the request become asynchronous and the callback function and onreadystatechange property must be used.

Click here to learn more about synchronous request.

Note:You might face the problem of browser stuck, slow website link with the synchronous request of the Ajax so you have keep these things into account before making the synchronous request.

12 thoughts on “How to return value from Ajax Function – Use synchronous request

  1. nice tutorial thanks. this is only the concept , when i use not wotking….still return value “undefined”.

    please check..again…

  2. Which function did u use the second one right? Did you made your own getXMLHTTP(); function

  3. Manish is right. The value returned is still undefined.

  4. @Scott – which function did you try the first one or the second one ? try the second one….

  5. Tony

    This is not working my friend, it keeps returning undefined.

  6. newbies

    hey thanks a lot for your tips, this helped me in my headache ajax in developing php database library..

    more power!

  7. drgiggles

    great stuff thanks for this works like a charm.

  8. it is great :)
    I have been trying to fix it for a long time …
    thank you

  9. haha

    with Prototype.js, below code works!

    function test(param) {
    var url = “haha”+param;
    new Ajax.Request(url,{
    asynchronous: false,
    onComplete: function(data) {
    result = data.responseText;
    return result;

  10. rbw152

    Brilliant! I’d been trying to work this one out all morning and you descrinbed precisely the problem I had.

    Thanks very much!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>