简体   繁体   English

如何从API获取图像

[英]How to fetch image from api

I got the image name in pic variable , but now i'm blank how to show that image in front end . 我在pic变量中得到了图像名称,但是现在我空白了如何在前端显示该图像。

<script>
$.getJSON(`https://openlibrary.org/authors/<%= match %>.json`, function(data) {


    var text = `<tr><td>${data.name}</td>
                    <td>${data.bio}</td>
                    <td>${data.alternate_names}</td>
                <td>${data.birth_date}</td></tr>`
    var pic = `${data.photos}`
    $(".mypanel").html(text);
    $(".picpanel").html(pic);

});

There are several ways, you can simply implement them in img tags : 有几种方法,您可以简单地在img标签中实现它们:

$(".picpanel").append("<img src='" + pic + "'>");

you can also implement it on a element as background : 您还可以将其作为背景实现在元素上:

$(".picpanel").css('background-image', 'url(' + pic + ')');

Edit: wanted solution : 编辑:想要的解决方案:

<script>
$.getJSON(`https://openlibrary.org/authors/<%= match %>.json`, function(data) {


    var text = `<tr><td>${data.name}</td>
                <td>${data.bio}</td>
                <td>${data.alternate_names}</td>
            <td>${data.birth_date}</td></tr>`
    var pic = data.photos;
    $(".mypanel").html(text);
    if(pic!==undefined && pic.length>0){
        $(".picpanel").html("<img src='//covers.openlibrary.org/a/id/" + pic[0] + "-M.jpg'>");
    }
});
</script>

就您当前的情况而言,我想可能是这样。

$(".picpanel").html("<img src='" + pic + "' />");

Here is your complete solution 这是您的完整解决方案

   <script>
          $.getJSON(`https://openlibrary.org/authors/<%= match %>.json`, function(data) {


    var text = `<tr><td>${data.name}</td>
                    <td>${data.bio}</td>
                    <td>${data.alternate_names}</td>
                <td>${data.birth_date}</td></tr>`
    var pic = `${data.photos[0]}`
    $(".mypanel").html(text);
    var add =pic+'-M.jpg'
    console.log(add);
    $(".picpanel").append("<img src='https://covers.openlibrary.org/a/id/" + add + "'>");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM