0

When i am trying to get json type data in return of an ajax call,i am getting undefined as return.

function AjaxCallToGetData(){
    var chartDataCheckin;
    var checkin_location = $("#select_checkin_location").val();
    jQuery.ajax({
        'url': '<?php echo $this->modurl; ?>dashboard/checkinchart',
        dataType: 'json',
        data: {checkin_location: checkin_location},
        'type': 'POST',
        success: function(data) {
            alert(data);     // alerts object  

            return data;
        }
    });

}
function getcheckinchart(DataCheckin) {
    alert('hi');
    var chartDataCheckin = AjaxCallToGetData();
    alert(chartDataCheckin); // this doesnot
    }

Please help

0

4 Answers 4

1
    var testobj;
function AjaxCallToGetData(){
        var chartDataCheckin;
        var checkin_location = $("#select_checkin_location").val();
        jQuery.ajax({
            'url': '<?php echo $this->modurl; ?>dashboard/checkinchart',
            dataType: 'json',
            data: {checkin_location: checkin_location},
            'type': 'POST',
            success: function(data) {
                alert(data);     // alerts object  

               testobj=data;
            }
        });

    }
    function getcheckinchart(DataCheckin) {
        alert('hi');
         AjaxCallToGetData();
        alert(testobj); 
        }

This will work :)Cheers

Sign up to request clarification or add additional context in comments.

Comments

0

You are getting the data from an async call to the server but you are expecting the return in a sync way. Use a global variable or use a Deferred Object or a Promise: https://api.jquery.com/category/deferred-object/

Comments

0

Your return statement is located in the success callback of the (asynchronous) AJAX action, so it isn't returned as result of AjaxCallToGetData().

The solution:

Either do your processing in the success callback or create your own callback. I myself am partial to dynamic callbacks so I included a basic example where I pass the callback function as a parameter.

function AjaxCallToGetData(callback){
    var chartDataCheckin;
    var checkin_location = $("#select_checkin_location").val();
    jQuery.ajax({
        url: '<?php echo $this->modurl; ?>dashboard/checkinchart',
        dataType: 'json',
        data: {checkin_location: checkin_location},
        type: 'POST',
        success: function(data) {
            // Either process the data here or:
            if (jQuery.isFunction(callback)) callback(data);
        }
    });
}

function getcheckinchart(DataCheckin) {
    // Notice we're passing the callback function as a parameter
    AjaxCallToGetData(processcheckinchart);
}

function processcheckinchart(data) {
    alert(data);     // alerts object
}

Comments

0

the ajax call is asynchronous so when you alert in the getcheckinchart the data are not still there. This should work.

 function AjaxCallToGetData(callback){
        var chartDataCheckin;
        var checkin_location = $("#select_checkin_location").val();
        jQuery.ajax({
            'url': '<?php echo $this->modurl; ?>dashboard/checkinchart',
            dataType: 'json',
            data: {checkin_location: checkin_location},
            'type': 'POST',
            success: function(data) {
                alert(data);     // alerts object  

                callback(null,data);
            }
        });

    }
    function getcheckinchart(DataCheckin) {
        alert('hi');
        AjaxCallToGetData(function(err,charDataCheckin){
            alert(chartDataCheckin); 
        });
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.