Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript json图像文件未在html中显示_Javascript_Html_Json_Image - Fatal编程技术网

Javascript json图像文件未在html中显示

Javascript json图像文件未在html中显示,javascript,html,json,image,Javascript,Html,Json,Image,我的服务器中有一个json文件,我试图在我的html网站中显示它,我执行了以下代码: var数据={ “图像”:[{ “横幅1”:http://molugu.com/yantraev/animation.json" }, ] }; data.images.forEach(函数(obj){ var img=新图像(); img.src=obj.bannerImg1; img.setAttribute(“类”、“横幅img”); img.setAttribute(“alt”、“effy”); do

我的服务器中有一个json文件,我试图在我的html网站中显示它,我执行了以下代码:

var数据={
“图像”:[{
“横幅1”:http://molugu.com/yantraev/animation.json"
},
]
};
data.images.forEach(函数(obj){
var img=新图像();
img.src=obj.bannerImg1;
img.setAttribute(“类”、“横幅img”);
img.setAttribute(“alt”、“effy”);
document.getElementById(“img容器”).appendChild(img);
});

元素用于显示图像

JSON是一种基于文本的序列化数据格式。这不是一个图像

通常,要显示JSON,您需要使用Ajax API(如
fetch
)请求它,然后根据JSON中编码的特定数据格式生成一些HTML元素,然后使用DOM方法插入这些元素。

请这样尝试

var data = {
  "images": [{
    "bannerImg1": "http://molugu.com/yantraev/animation.json"
  },
  ]
};
data.images.forEach( function(obj) {

   fetch(obj.bannerImg1, 
        {
            method: "GET", 
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json',
            }
        }
    ).then(response => response.json())
    .then(data => {
       data.assets.forEach(function(image){
          img.src = image.p;
          img.setAttribute("class", "banner-img");
          img.setAttribute("alt", "effy");
          document.getElementById("img-container").appendChild(img);
       });

    })
    .catch((err) => {
        console.log(err);
    })
 

});
<div class="banner-section" id="img-container">
 </div>
var数据={
“图像”:[{
“横幅1”:http://molugu.com/yantraev/animation.json"
},
]
};
data.images.forEach(函数(obj){
获取(对象1,
{
方法:“获取”,
模式:“cors”,
标题:{
“内容类型”:“应用程序/json”,
}
}
).then(response=>response.json())
。然后(数据=>{
data.assets.forEach(函数(图像){
img.src=image.p;
img.setAttribute(“类”、“横幅img”);
img.setAttribute(“alt”、“effy”);
document.getElementById(“img容器”).appendChild(img);
});
})
.catch((错误)=>{
控制台日志(err);
})
});
您用来解释这些问题的示例文件不起作用。示例json文件似乎是从lottie导出的动画json文件

请查看此小提琴以获得实时实现

const logoSvgData='1〕http://molugu.com/yantraev/animation.json';
const animation=bodymovin.loadAnimation({
容器:document.getElementById('logo-bm'),
渲染器:“svg”,
循环:对,
自动播放:对,
动画数据:logoSvgData
})
#标志bm{
宽度:400px;
高度:自动;
}


我是新来的,你能不能显示为AxSample或给我参考链接试用过的bro,不工作的bro我只想显示json文件,jsonLet中的动画让我试试,等一下检查一下,看看实现