0

I have made a json call from this type of function and i got the result, But i cannot return this value to another function

get_target_amount(full)

this is my function. after console.log(get_target_amount(full)) result is undefined. what is the reason for this?

 function get_target_amount(full) {
            var target = '';
            var param = {};
            param.sub_category_id = full["sub_category_id"];
            param.project_month = full["project_month"];
            jQuery.post(site_url + '/Progress_project_create/get_target_amount', param, function (response) {

                        if (response !== null) {
                    target = response.target_amount;

                } else {
                    target = 'Target amount not found';
                }
                return target;
            }, 'json');
        }

1 Answers1

0

The problem here is the asynchronous nature of Ajax calls.

Please move your target variable based code into the callback function you have in the get() function.

function(response){//Write all your code here.}

Currently, the get_target_amount() returns back without waiting for the ajax call to finish. Hence, you get the null value.

You can consider another option like, making you ajax call synchronous. Then the browser halts/freezes the page till the ajax function completes. This will not be advisable as it makes the user unnecessarily wait for the page to respond.

Also, the return statement you have in your callback function is not referred anywhere