链接到JSON数据 - > http://starlord.hackerearth.com/sureify/cokestudio 在浏览器中执行程序时,它显示URL而不是像这样的图像或音频 - >
我的代码:
<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>
答案 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);
}
}