0

I have a function that will return an AJAX response and use it on another function. Here is my code:

function updateLoanApproval(answer){
    var password = document.getElementById("password").value;
    var usid; 
    getExistingId(function(success){
        if (success === 0){
            var usid = "No ID Available";
        } else {
            var usid = success; //To get the returning value
        }
    });

    alert(usid);

And here is the code for getExistingId()

function getExistingId(){
    var url =  "../../library/functions.php?action=getId";
    var uid;
    http.onreadystatechange = function(){
        if (http.status == 200 && (http.readyState === 4)){
            uid = http.responseText;
            if (uid == "No ID"){
                callback(0);
            } else {
                callback(id);
            }
        }
    }

    http.open("GET",url,true);
    http.send();
}

As I test the code, I don't have a problem with a query or PHP code so I will not include it here, but why is usid always return undefined?

halfer
  • 19,824
  • 17
  • 99
  • 186
Niel Sinel
  • 291
  • 2
  • 6
  • 15

1 Answers1

1

See carefully, You are missing callback parameter in getEdiistingId() function. Do like following:

function getExistingId(callback){
var url =  "../../library/functions.php?action=getId";
var uid;
http.onreadystatechange = function(){
    if (http.status == 200 && (http.readyState === 4)){
        uid = http.responseText;
        if (uid == "No ID"){
            callback(0);
        } else {
            callback(id);
        }
    }
}

http.open("GET",url,true);
http.send();

}

And put alert inside callback method.

function updateLoanApproval(answer){
    var password = document.getElementById("password").value;
    var usid; 
    getExistingId(function(success){
        if (success === 0){
            var usid = "No ID Available";
        } else {
            var usid = success; //To get the returning value
        }
        alert(usid);
    });

A method that need usid:

function doMyTaskThatNeedUsid(usid)
{
    // do something.
}

And call method like following:

function updateLoanApproval(answer){
    var password = document.getElementById("password").value;
    var usid; 
    getExistingId(function(success){
        if (success === 0){
            var usid = "No ID Available";
        } else {
            var usid = success; //To get the returning value
        }

        // Calling method that need usid.
        doMyTaskTaskThatNeedUsid(usid);
    });
Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • Still the output is undefined :( – Niel Sinel Jul 21 '15 at 02:06
  • Have you put alert in updateLoadApproval method – Sachin Chandil Jul 21 '15 at 02:07
  • It's now working but is there any possible solution if i want the value of the varilable usid works around the function outside the function call on updateLoanApproval? – Niel Sinel Jul 21 '15 at 02:09
  • See you are making an sync task that will run after response from server is received. You have to call your target method inside callback method. – Sachin Chandil Jul 21 '15 at 02:12
  • @FelixKling sorry, I'm just a beginner on using ajax :( – Niel Sinel Jul 21 '15 at 02:15
  • Is it impossible that the value of the variable `usid` is still useful outside the call? My example above is just an example run to test if I can get the ID from the request. On the other hand, I will use more codes below and I really need the value of that `usid` to perform more functions and queries. If I put the other codes insides the call of getExistingId() call, I think my code will become more chaotic, I don't know. – Niel Sinel Jul 21 '15 at 02:19
  • Thanks for the help :) – Niel Sinel Jul 21 '15 at 02:26
  • The invocation of callback method that is assigned to you ajax request will be performed when response is received. you can access `usid` outside of callback but after callback method is called(response is received). Assume there is a network problem so response may delay that will make it impossible to use `usid` outside of callback and tracking that is non sense if you have a better alternative. – Sachin Chandil Jul 21 '15 at 02:30