2013-02-01 38 views
1

我敢肯定,這可能是我愚蠢的,但我想確認這個問題。我正在構建一個Windows 8應用程序,並從MySQL服務器加載數據,我似乎無法解決此問題。從一些閱讀中,我猜測問題是代碼在json請求之前執行是正確的,並且有辦法解決它。返回值在那裏,因爲它在一個返回數據並顯示它的函數中。 「SampleItems [0]」顯示完美,但JSON內部的東西不會,但控制檯日誌顯示它正在從服務器獲取數據。預先感謝任何幫助!JSON問題與執行

var sampleItems = []; 
    sampleItems[0] = { group: sampleGroups[count], title: "New Image", subtitle: "", description: "", content: "", backgroundImage: "/images/add.png" }; 

     //this code calls to our cloud based service which gets the data which is needed and returns it in a JSON object back for this to handle 
     $.getJSON('http://pumpstationstudios.com/tumapics/services/getimages.php', function (data) { 
      var service_url = 'http://pumpstationstudios.com/tumapics/'; 
      var count = 1; 


      images = data.items; 
      $.each(images, function (index, image) { 
       image.image = service_url + image.image; 
       sampleItems[count] = { group: sampleGroups[count], title: "Hello", subtitle: "", description: "", content: "", backgroundImage: image.image }; 
       count++; 
      }); 
     }); 

    return sampleItems; 

回答

1

碰巧,我answered一個類似的問題javascript code execution and ajax async幾個小時前

從本質上講,什麼情況是

$.getJSON('http://.../getimages.php', function(data) { 
// process data 
}); 

// this runs *before* "process data" 
return sampleItems; 

這意味着,你回到sampleItems,之前它是由getJSON填寫回調函數。

要解決此問題,您必須在回調函數中執行處理。

+0

謝謝你這麼做了一個夢! – jayhassett89