如何从JSON数据中给出的URL显示图像和播放音频?

时间:2017-06-04 07:18:47

标签: javascript json

链接到JSON数据 - > http://starlord.hackerearth.com/sureify/cokestudio 在浏览器中执行程序时,它显示URL而不是像这样的图像或音频 - >

http://hck.re/3Cm0IX

http://hck.re/5dh4D5

http://hck.re/U1bRnt

http://hck.re/eLtjUb

我的代码:

<body>

<div id="cover_img" width="150px" height="150px">  </div> 

<script type="text/javascript">
    var requestURL = 'http://starlord.hackerearth.com/sureify/cokestudio';

    var request = new XMLHttpRequest();

    request.open('GET', requestURL);

    request.responseType = 'json';

    request.send();

    request.onload = function(){
        var myjsondata = request.response; //request.response contains all our JSON data
        showdata(myjsondata);


    } 

    function showdata(data){

        for(var i=0;i<data.length;i++){
            document.write(data[i].cover_image +"<br>");
        }
    }
</script>
</body>

1 个答案:

答案 0 :(得分:1)

您需要创建<IMG>代码并填充其src属性,如下所示:

function showdata(data){

    for(var i=0;i<data.length;i++){
        document.write("<br/>");
        document.write(data[i].cover_image +"<br>");
        var str = "<IMG src='"+data[i].cover_image+"'/>";
        document.write(str);
        document.write("<br/>");
        str = "<a href='"+data[i].url+"'>"+data[i].song+"</a>";
        document.write(str);
    }
}