2012-07-30 102 views
0

我無法返回一個JSON對象,而是我得到的是一個未定義的變量。我不能返回我的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; 
     } 
    }); 
}; 

任何幫助,將不勝感激。

SOLUTION

感謝所有幫助過我的人。

這似乎這樣的伎倆

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

[返回AJAX回調回報(http://stackoverflow.com/questions/9041321/return-ajax-callback-return) – xdazz 2012-07-30 09:47:27

+1

可能重複的可能重複[jQuery AJAX:成功返回值](http://stackoverflow.com/questions/9055810/jquery-ajax-return-value-on-success)和[許多其他](http://stackoverflow.com/search?q = jquery + return + value + from + ajax&submit = search)... – 2012-07-30 09:49:23

+0

上述代碼將工作的唯一方法是在$ .ajax選項中將async設置爲false。儘管這會導致瀏覽器凍結,直到完成ajax調用 – 2012-07-30 09:51:54

回答

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

這將返回undefined - ajax調用不能是異步的。 – Summoner 2012-07-30 10:04:34

+0

每一個,但如果我添加「異步:錯誤」,這應該做的工作。謝謝 – HeM01 2012-07-30 10:23:18

+0

謝謝,這工作的魅力! – 2012-07-30 11:21:10

1

AJAX調用都是異步的,你必須使用以下方法返回從AJAX調用的響應:

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

我不確定responseText是否會被解析或解析爲JSON,但看看你從哪裏得到。

0

相反的:

return jQuery.parseJSON(data); 

OR

return data; 

直接調用createChart功能:

createChart(data)