从外部 json 文件获取数据

Getting data from external json file

本文关键字:数据 获取 文件 json 从外部      更新时间:2023-09-26

我目前正在使用 JQuery 读取包含照片幻灯片数据的外部 .json 文件

照片幻灯片.json

 [ 
        {
            "title" : "my tile", 
            "image" : "xx.jpg", 
            "url" : "www.example.com",
            "firstline" : "woow", 
            "secondline" : "the weather is fine"
        },
       .....
       ..... 
    ]
    <script type="text/javascript" >
    var photos; 
    $.getJSON(
           "lang/en/photo-slideshow.json", 
            function(result) {
                    photos = result;
            }
       );
    <script>

该脚本在加载时间较长的页面上工作正常!页面加载速度非常快(例如 1 秒)JSON 文件t seem to be read completely and the slideshow doesn无法启动。

我解决了! 我把这一行放到我的代码中: $.ajaxSetup({async:false});谢谢

是否在

触发 json 回调后初始化幻灯片?

当您对 json 对象执行异步调用时,幻灯片初始化应该在 getJson 触发的回调函数中,以确保您在需要时确实拥有数据。

你应该做这样的事情:

$.getJSON('path/to/my.json',function(result){
    mySlideShowComponent.init(result);
})