简体   繁体   中英

Automatically pass the retrieved value from a javascript function to another javascript function?

I have two inline java script functions, one intended to get a token value using xhr and second one will use that value to get the full response from server. But when i uploaded the same on server nothing happened.

Here goes the sample code:

function getUser(user)
      {


   var xmlhttp;
   var result,x,i;
   var uservalue=user;
   var url="http://abc.xyz.com:8090/uauth/" +uservalue;
   ///here starts the getToken method..
   var tokensave=function getToken(uservalue)
                    {
                     var xmlhttp;
                     var token,a,b;
                     var url="http://abc.xyz.com:8090/uauth/" +uservalue;
                     var data="op=login&pass=" +uservalue;   


                     if (window.XMLHttpRequest)
                        {// code for IE7+, Firefox, Chrome, Opera, Safari
                         xmlhttp=new XMLHttpRequest();

                                 if (xmlhttp==null)
                                    {
                                     alert ("Browser does not support HTTP Request");
                                     return;
                                     }

                         }
                    else
                         {// code for IE6, IE5

                          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                         }

                    xmlhttp.open("POST",url, true);

                    xmlhttp.onreadystatechange=function()
                         {

                                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                   {

                                    xmlDoc=xmlhttp.responseXML;
                                    token="";
                                    a=xmlDoc.getElementsByTagName("token");

                                    token=token + a.childNodes.nodeValue;

                                        document.getElementById("myDiv").innerHTML=token;
                                    }
                          };

                     xmlhttp.setRequestHeader("Content-Type", "text/xml");   
                     xmlhttp.send(data);
                     return token;

                 }


   var header="Xyz-Authorization: "+tokensave;


                     if (window.XMLHttpRequest)
                        {// code for IE7+, Firefox, Chrome, Opera, Safari
                         xmlhttp=new XMLHttpRequest();

                     if (xmlhttp==null)
                        {
                        alert ("Browser does not support HTTP Request");
                        return;
                        }

                        }
                     else
                        {// code for IE6, IE5
                         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }

                         xmlhttp.open("GET",url, true);

   xmlhttp.onreadystatechange=function()
                        {
                            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                               {

                               xmlDoc=xmlhttp.responseXML;
                               result="";
                               x=xmlDoc.getElementsByTagName("response");
                               for (i=0;i<x.length;i++)
                                    {
                                     result=result + x[i].childNodes[0].nodeValue + "<br />";
                                    }
                               document.getElementById("myNewDiv").innerHTML=result;
                               }
                         };

   xmlhttp.setRequestHeader("Xyz-Authorization: ", +tokensave);
   xmlhttp.send();


 }
   HTML CODE:
     <div id="myDiv"></div>
    <br />
    <br />
     <div id="myNewDiv"></div > 
     <br />
     <form>
     <input name="textbox" id="textbox" type="text" />
     <input name="buttonExecute" onclick="getUser(document.getElementById('textbox').value)" type="button" value="Get User"    />
     </form>

What's wrong with this apparoach?

Thanks in advance for your valuable help...
mrana

您需要在第一个函数中使用return token ,并像这样使用它getUser(getToken(user))

Just create a global variable, and you set change the value inside de function but it's still accesible from a global scope for all the functions you want.

var data = false;

function getData() {
    // some ajax...
    data = {cool:true};
}

function foo () {
    if(data.cool) {
        alert (data.cool)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM