我不能回到我的JSON对象 [英] I can't return my json object

查看:220
本文介绍了我不能回到我的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这code应该从API返回一个JSON元素。这似乎在成功的工作:功能,但一旦我尝试和采取其他的数据,它只是显示不确定

  VAR DATAS;
DATAS =的getData();
//的getData();
createChart(DATAS)警报('2datass'+ DATAS); //这个返回undefined功能createChart(数据){
    警报(数据); //输出未定义
}功能getdataaa(){
    警报('AJAX');
    $阿贾克斯({
        键入:GET,
        数据类型:JSON
        网址:API URL,
        成功:功能(数据){            警报(数据); // WORKS!输出我的JSON数据
            / *
              对于(VAR I = 0;我LT&= data.length-1;我++)
              {
              VAR项目=数据[I]              DATAS = DATAS + {
              类型:列,
              名称:item.name,
              数据:[item.difference]
              };
              }
            * /            //无论这些收益似乎工作
            返回jQuery.parseJSON(数据);
            返回的数据;
        }
    });
};

任何帮助将是AP preciated。

SOLUTION

感谢所有谁都有帮助。

这似乎这样的伎俩

  VAR DATAS;
        DATAS =的getData();
        //的getData();        警报('2datass'+ DATAS);
        的console.log(DATAS);
        createChart(DATAS);
        功能createChart(数据){            警报('createChart'+数据);
            VAR dynamicData;
                    对于(VAR I = 0;我LT&= data.length-1;我++)
                    {
                        VAR项目=数据[I]                        dynamicData = dynamicData + {
                            类型:列,
                            名称:item.name,
                            数据:[item.difference]
                        };                    }            警报('动态'+ dynamicData); //工作,但是他说,以前未定义 - 输出动态不确定[对象的对象] [对象的对象] [对象的对象] [对象的对象] [对象的对象]            VAR系列= [dynamicData,{
                    类型:列,
                    名称:'简,
                    数据:[300,30]
                },{
                    类型:列,
                    名称:'约翰',
                    数据:[-200,50]
                },{
                    类型:列,
                    名称:'乔',
                    数据:[444,-25]
                },{
                    类型:列,
                    名称:乔布',
                    数据:[444,-25]
                },{
                    类型:列,
                    名称:'Jooe',
                    数据:[444,-25]
                },{
                    类型:列,
                    名称:'简,
                    数据:[300,30]
                }
                {
                    类型:'馅饼',
                    名称:'总消费,
                    数据:[{
                            名称:'简,
                            Y:13
                            //颜色:#4572A7//简的颜色
                        },{
                            名称:'约翰',
                            Y:23
                            //颜色:#AA4643'//约翰的颜色
                        },{
                            名称:'乔',
                            Y:19
                            //颜色:#89A54E'//乔的颜色
                        }],
                    中心:[30,80],
                    尺寸:100,
                    showInLegend:假的,
                    dataLabels:{
                        启用:假的
                    }
                }];
            VAR的选择= {
                图:{
                    renderTo:容器
                },
                标题:{
                    文字:客户经理排​​行榜
                },
                X轴:{
                    类别:逐月,一天一天]
                },
                提示:{
                    格式:函数(){
                        变种S;
                        如果(this.point.name){//饼图
                            S =''+
                                this.point.name +:+ this.y +'销售';
                        }其他{
                            S =''+
                                this.x +:+ this.y;
                        }
                        返回S;
                    }
                },
                标签: {
                    项目:[{
                            HTML:销售和LT的总比例; BR />这一个月,
                            风格:{
                                左:40点,
                                顶部:-5px',
                                颜色:黑
                            }
                        }]
                },
                系列:系列
            };            $(文件)。就绪(函数(){
                VAR图;
                图=新Highcharts.Chart(选件);
            });        }       函数的getData(){
            //警报('AJAX');            VAR receivedData; //保存你的价值在这里
            $阿贾克斯({
                键入:GET,
                数据类型:JSON
                网址:API URL,
                异步:假的,
                成功:功能(数据){
                    警报('数据'+数据); //作品
                    receivedData =数据;
                }
            });            返回receivedData;
        };


解决方案

的问题是,您从成功返回函数,而不是你的 getdataaa 功能。 getdataaa 没有return语句,所以它返回未定义默认情况下。 Return语句是指最近的功能。

 成功:功能(数据){
   警报(数据); // WORKS!输出我的JSON数据
   ...
   //无论这些收益似乎工作
   返回jQuery.parseJSON(数据);
   返回的数据;
}

这是你会得到什么。您可以使用闭包像这样得到你的结果:

 函数getdataaa(){
    警报('AJAX');    VAR receivedData; //保存你的价值在这里
    $阿贾克斯({
        键入:GET,
        数据类型:JSON
        网址:API URL,
        异步:假的,
        成功:功能(数据){            警报(数据);            // ...            receivedData =数据;
        }
    });    返回receivedData;
};

I'm having trouble returning a JSON object, instead all I get is an undefined variable.

This code is supposed to return a json element from an API. It seems to work in the success: function, but once I try and take that data elsewhere it just displays 'undefined'

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;
        }
    });
};

Any help would be appreciated.

SOLUTION

Thanks to all who have helped.

This seems to do the trick

        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;
        };

解决方案

The problem is that you return from success function, not your getdataaa function. getdataaa doesn't have a return statement, so it returns undefined by default. Return statement refers to the nearest function.

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

This is what you get. You can get your result using a closure like this:

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;
};

这篇关于我不能回到我的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆