0

Am trying to store Client Details In a Variable and display it for which i have used following code

let val = [];
val = get_clientDetails({"recursive":false});

console.log(val) // It's Prints Undefined.
function get_clientDetails(param)

    if(param.recursive == true){
        
        console.log(param.data); // It prints the ajax response. It works good till here. 
        return param.data;

    } else{

        let objClientDetails = new clientDetails();
        objClientDetails.client_details();        

    }

}
class clientDetails(){


client_details(){
      
        $.ajax({

            url: 'http://108.157.126.128:/clients',
            method: 'GET',
            dataType: 'JSON',
            contentType: 'application/json',
            async:false,
            headers: {
                "Authorization": 'Bearer '+sessionStorage.getItem('access_token')},
            success: function(client_data){

                
                get_clientDetails({"recursive": true, "data": client_data});



            }
        });
        
    }

}

When the above code Gets executed am unable to print the value returned from the callback function. The code works good till we print the response in the callback function I.E get_clientDetails(). But when the function returns the value it prints as undefined.

Dilkush
  • 113
  • 1
  • 2
  • 15

1 Answers1

0
            class clientDetails {

                client_details() {

                    var result;

                    $.ajax({
                        url: 'http://108.157.126.128:/clients',
                        method: 'GET',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false, //>>>>>> ALWAY IN MODE FALSE.
                        headers: {
                            "Authorization": 'Bearer ' + sessionStorage.getItem('access_token')},
                        success: (client_data) => {

                            result = get_clientDetails({"recursive": true, "data": client_data});
                        },
                        error: (a) => {

                            result = "error";
                        }
                    });

                    return result;

                }

            }

            function get_clientDetails(param) {


                    if (param.recursive === true) {

                        console.log(param.data); // It prints the ajax response. It works good till here. 
                        return param.data;


                    } else {

                        let objClientDetails = new clientDetails();
                        return objClientDetails.client_details();

                    }

             }

             var result = get_clientDetails({"recursive": false});

             alert(result);
toto
  • 1,180
  • 2
  • 13
  • 30
  • Hello @toto, Your above Code Works fine, But i want to save the result directly in a variable when get_clientDetails() is called. Is there a better way to achieve this. Thank you. – Dilkush Jul 18 '20 at 10:51
  • Hello, The other way is using Global variable scope. Confirm me if you need an example. – toto Jul 18 '20 at 15:06