0

I am retrieving response value from an ajax GET. I am trying to return the value using a callback function but its not returning the value. BUt when I output it to the console.log the value is shown.

Can anyone please help me return the value?

Any help would be really appreciated!!

This is not a duplicate because I am using a CALLBACK as opposed to the other question where there was no CALLBACK being used

// First Return the current logged hours when user wants to update the logged hours

 $(document).on("click",".update_logged_hrs", function () {

     var cur_task_hrs =   getLatestCatHrs(cat_id, function (hrs) {
                return hrs; // Not returning
            });   
console.log(cur_task_hrs)//Returns undefined

}):

//Call back function goes here

     function getLatestCatHrs(cat_id,callback){
            $.get("<?=base_url().ProjCollabNavLinks::$proj_todos?>",{

                latest_logged_cat_hrs : true,
                cat_id                : cat_id

            })

                .done(function (res) {

                    callback(res);

                });

        }
json2021
  • 2,146
  • 2
  • 14
  • 28
  • `var cur_task_hrs = getLatestCatHrs(...)` assigns the return value of `getLatestCatHrs` to `cur_task_hrs`. `getLatestCatHrs` doesn't return anything though. The callback returns something, but you are not calling the callback. The callback is called internally by `$.get`. There is no way for you to get that value. Additionally, Ajax is asynchronous, as you can learn about in the linked question. – Felix Kling Nov 07 '15 at 04:25
  • 2
    This is absolutely a duplicate. I'm using a callback there as well: `success: function(response) { ... }`. Take your time to read it and to understand the problem. – Felix Kling Nov 07 '15 at 04:27

0 Answers0