来自XMLHttpRequest的变量

I'm creating a function (mother) javascript that goes inside to call another function (ajax). The ajax function sends the data to an asp page The answer I need inside then the function mother

to call the function ajax so I write (in the function mother)

                  prendoDato=sendRequest(url,handleRequest);
                  alert(" prendoDato  " + prendoDato)

while the function is this ajax

   function handleRequest(req) {
   }

   function sendRequest(url,callback,postData) {
       var req = createXMLHTTPObject();
       if (!req) return;
       var method = (postData) ? "POST" : "GET";
       req.open(method,url,true);


     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       if (postData)
           req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
       req.onreadystatechange = function () {
           alert('HTTP error ' + req.status);
           if (req.readyState != 4) return;
           if (req.status != 200 && req.status != 304) {
          //          alert('HTTP error ' + req.status);
               return;
           }
           if (req.status == 200)
           {
               alert("req " +req.responseText)
               return req.responseText
           }
           callback(req);

       }
       if (req.readyState == 4) return;
       req.send(postData);
          }


          var XMLHttpFactories = [
       function () {return new XMLHttpRequest()},
       function () {return new ActiveXObject("Msxml2.XMLHTTP")},
       function () {return new ActiveXObject("Msxml3.XMLHTTP")},
       function () {return new ActiveXObject("Microsoft.XMLHTTP")}
   ];


   function createXMLHTTPObject() {
       var xmlhttp = false;
       for (var i=0;i<XMLHttpFactories.length;i++) {
           try {
               xmlhttp = XMLHttpFactories[i]();
           }
           catch (e) {
               continue;
           }
           break;
       }
       return xmlhttp;
   }

the problem I have is this: I call the function ajax and I would put the return value in a variable in the function mother the problem and that the ajax function but passes the value to the variable undefined to make sure I put the alert and practically I get the first alert that is after the function call ajax and then the alerts that are in the ajax function, after which intercepts the value, but that is no longer able to pass it to the variable prendoDato

I tried to put a do loop in the function mother, but goes in a continuous loop

  do {

            cccc=sendRequest(url,handleRequest);
            alert("cccc "+cccc)
        }
        while (cccc !== "undefined");

problem solved

because of an asynchronous request

I settled with a callback

place below the function and how the call

function call_ajax(urlPage,postdata){
    $.ajax({
    type: "POST",
    url: urlPage,
    dataType: "html",
    data: postdata,
    async: false,
    cache: false, 
    timeout:30000,
    success: function(html){
    //alert("html " + html);
    //return html

    obj = html


    }
    });


    if(obj =='"empty"'){
    return false;
    }else{
    return obj;
    }

    }






    url="/include/ajaxTempoDistanza.asp"
    postData="origine=43.608896,3.880175&destintario=4.60736,3.8939890000000332&stampa=no"  

    sp=call_ajax(url,postData)
    alert(" sp " + sp)