0

我无法返回 JSON 对象,而我得到的只是一个未定义的变量。

此代码应该从 API 返回一个 json 元素。它似乎在成功中起作用:函数,但是一旦我尝试将该数据带到其他地方,它只会显示“未定义”

var datas;
datas = getdata();
//getdata();
createChart(datas)

alert('2datass'+datas); // this returns undefined

function createChart(data){
    alert(data); //outputs undefined
}

function getdataaa(){
    alert('ajax');
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "API URL",
        success: function(data){

            alert(data); // WORKS! and outputs my JSON data
            /*
              for(var i =0;i <= data.length-1;i++)
              {
              var item = data[i];

              datas = datas + {
              type: 'column',
              name: item.name,
              data: [item.difference]
              };
              }
            */

            //Neither of these returns seems to work
            return jQuery.parseJSON(data);
            return data;
        }
    });
};

任何帮助,将不胜感激。

解决方案

感谢所有帮助过的人。

这似乎可以解决问题

        var datas;
        datas = getData();
        //getdata();

        alert('2datass'+datas);
        console.log(datas);
        createChart(datas);


        function createChart(data){

            alert('createChart'+data);
            var dynamicData;


                    for(var i =0;i <= data.length-1;i++)
                    {
                        var item = data[i];

                        dynamicData = dynamicData + {
                            type: 'column',
                            name: item.name,
                            data: [item.difference]
                        };

                    }

            alert('dynamic' +dynamicData); // works, but says undefined before - outputs dynamic undefined[object Object][object Object][object Object][object Object][object Object]



            var series = [dynamicData,{
                    type: 'column',
                    name: 'Jane',
                    data: [300, 30]
                }, {
                    type: 'column',
                    name: 'John',
                    data: [-200, 50]
                }, {
                    type: 'column',
                    name: 'Joe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jobe',
                    data: [444, -25]
                }, {
                    type: 'column',
                    name: 'Jooe',
                    data: [444, -25]
                },{
                    type: 'column',
                    name: 'Jane',
                    data: [300, 30]
                }
                , {
                    type: 'pie',
                    name: 'Total consumption',
                    data: [{
                            name: 'Jane',
                            y: 13
                            //color: '#4572A7' // Jane's color
                        }, {
                            name: 'John',
                            y: 23
                            //color: '#AA4643' // John's color
                        }, {
                            name: 'Joe',
                            y: 19
                            //color: '#89A54E' // Joe's color
                        }],
                    center: [30, 80],
                    size: 100,
                    showInLegend: false,
                    dataLabels: {
                        enabled: false
                    }
                }];




            var options = {
                chart: {
                    renderTo: 'container'
                },
                title: {
                    text: 'Account Managers Leaderboard'
                },
                xAxis: {
                    categories: ['Month on Month', 'Day on Day']
                },
                tooltip: {
                    formatter: function() {
                        var s;
                        if (this.point.name) { // the pie chart
                            s = ''+
                                this.point.name +': '+ this.y +' sales';
                        } else {
                            s = ''+
                                this.x  +': '+ this.y;
                        }
                        return s;
                    }
                },
                labels: {
                    items: [{
                            html: 'Total proportion of sales <br />this month',
                            style: {
                                left: '40px',
                                top: '-5px',
                                color: 'black'
                            }
                        }]
                },
                series: series
            };

            $(document).ready(function() {
                var chart;
                chart = new Highcharts.Chart(options);
            });

        }



       function getData(){
            //alert('ajax');

            var receivedData; // store your value here
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "API URL",
                async: false,
                success: function(data){
                    alert('data'+data); //works
                    receivedData = data;
                }
            }); 

            return receivedData;
        };
4

3 回答 3

1

默认情况下,AJAX 调用是异步的,您必须使用以下方法从 ajax 调用返回响应:

function getdataaa() {
    return $.ajax({ 
        type: "GET",
        dataType: "json",
        url: "API URL",
        async: false // NOTICE THIS
    }).responseText; // AND THIS
};

我不确定 responseText 是否会被解析或未解析的 JSON,但看看你从哪里得到。

于 2012-07-30T09:47:20.383 回答
0

代替:

 return jQuery.parseJSON(data);

或者

 return data;

直接调用createChart函数:

createChart(data)
于 2012-07-30T09:52:17.527 回答
0

问题是你从success函数返回,而不是你的getdataaa函数。getdataaa没有返回语句,所以undefined默认返回。return 语句是指最近的函数。

success: function(data){
   alert(data); // WORKS! and outputs my JSON data
   ...
   //Neither of these returns seems to work
   return jQuery.parseJSON(data);
   return data;
}

这就是你得到的。您可以使用这样的闭包获得结果:

function getdataaa(){
    alert('ajax');

    var receivedData; // store your value here
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "API URL",
        async: false,
        success: function(data){

            alert(data);

            // ...

            receivedData= data;
        }
    });

    return receivedData;
};
于 2012-07-30T09:54:48.417 回答