1

in this code this method return undefined despites alert statement print a value ?

function getNearestPoint(idd) 
        {
            var xmlhttp;
            var result;
            if(window.XMLHttpRequest)
                xmlhttp=new XMLHttpRequest();
            else
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");     
            xmlhttp.onreadystatechange=function()
            {
             if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        result= xmlhttp.responseText;
                        alert(result);

                    }

            }
            xmlhttp.open("GET","ajax_get_nearest_location.php?id="+idd +"&radius=1",true);
            xmlhttp.send();
            return result;
        }
3
  • 3
    Because your function returns before a result is returned from the AJAX request. The request is Asynchronous. Commented Apr 13, 2011 at 22:38
  • I'm afraid people will never stop asking this kind of questions.... :-/ We have to start some campaign. Commented Apr 13, 2011 at 22:54
  • @Felix we need a reference question that we all can link to (or close as a duplicate of) :) Commented Apr 13, 2011 at 23:22

3 Answers 3

2

result isn't defined at that point, it only gets defined once your callback executes. The order of execution:

  • getNearestPoint starts
  • XHR is fired off
  • getNearestPoint returns undefiend
  • XHR comes back and runs xmlhttp.onreadystatechange
  • result gets set

If you need result from OUTSIDE of this, you should use a callback:

getNearestPoint(idd, cb){
   ...
   xmlhttp.onreadystatechange = function(){
      ...
      cb(result);
   }
}

and your calling code changes from:

var result = getNearestPoint(id);

to:

getNearestPoint(id, function(result){
   // do something with result;
});
Sign up to request clarification or add additional context in comments.

2 Comments

i want to call it like this way ? var url="getShortestPath.php?from="+getNearestPoint(id1)+"&to="+getNearestPoint(id2); how can i do that ?
@adham: That is not possible with asynchronous calls.
1

This is because of the asychronous nature of Ajax ("Asynchronous JavaScript and XML"): The request will still be running when your code hits return result. The readystatechange callback will not have been called yet, and the result variable not set yet.

The usual way to deal with this is to change the architecture of the script: Do whatever you need to do based on result directly in the onreadystatechange callback. (or, of course, pass a callback function with the desired actions and execute it in the handler.)

1 Comment

The only downside to running code directly in onreadystatechange is that it kills re-usability of the code. Better, in general, to use a callback
0

result setting statement executes inside async Ajax function

Because your second if statement (that sets result value) doesn't get hit. Why not? Because the moment you send an Ajax request you return the result value which is still undefined. Ajax call will execute the anonymous function later on and set this result variable which has been returned long ago.

Ajax is asynchronous and your code isn't taking this into account.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.