2013-02-01 37 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