heckerf
heckerf

Reputation: 33

JavaScript variable not correctly interpreted

In the header section of a HTML page, I have the following JavaScript-/Ajax-Function:

function getDataFromBackend(cmp){

$.ajax({
    method: 'GET',
    url: 'getRun.php?run='+cmp,
    dataType: 'script',
    success: loadData
});}

getRun returns an array like [[2, 4, 5, 57]]

loadData is defined as follows:

                function loadData(dataFromAjax){

                    setTimeout(function () {
                        console.log(dataFromAjax);
                        chartRunnerDist.load({
                            columns: dataFromAjax
                        });     
                    }, 100);
            }

In the console, I see the correct values. If I copy the console content behind columns: part manually, everything is fine. But dataFromAjax doesn't seem to be interpreted correctly. Do I have to explicitly convert it to an array?

Upvotes: 0

Views: 64

Answers (2)

Valentin Garcia
Valentin Garcia

Reputation: 495

try this

function loadData(dataFromAjax){
        setTimeout(function () {
            console.log(dataFromAjax);
            chartRunnerDist.load({
                columns: dataFromAjax[0]
            });     
        }, 100);
}

Upvotes: 0

Kristianmitk
Kristianmitk

Reputation: 4778

You may have to parse the response to convert it from a string to an object. Use JSON.parse(dataFromAjax)

Upvotes: 1

Related Questions