【问题标题】:JSON issue with executionJSON 执行问题
【发布时间】:2013-02-01 10:57:38
【问题描述】:

我确定这可能是我很愚蠢,但我想确认这个问题。我正在构建一个 Windows 8 应用程序并从 mysql 服务器加载数据,但我似乎无法解决这个问题。从一点阅读我猜问题是代码在 json 请求正确之前正在执行,有没有办法解决它。 return 在那里,因为它在一个函数中,该函数返回数据然后显示它。 “SampleItems [0]”显示完美,但 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;

【问题讨论】:

    标签: php ajax windows json


    【解决方案1】:

    碰巧,几个小时前我answered 一个类似的问题javascript code execution and ajax async

    基本上,发生的事情是

    $.getJSON('http://.../getimages.php', function(data) {
    // process data
    });
    
    // this runs *before* "process data"
    return sampleItems;
    

    这意味着,在getJSON 回调函数填充它之前,您返回sampleItems

    要解决这个问题,你必须在回调函数中进行处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多