简体   繁体   English

如何显示从调用 API 到 HTML 的图片?

[英]How to show picture from call API to HTML?

Issue is that the image is not showing up in my HTML.问题是图像没有出现在我的 HTML 中。 How to resolve this issue?如何解决这个问题? Please refer below.请参考下文。

图片不显示

This is my codes for the website i use to show the image up这是我用来显示图像的网站的代码

<div class="col-md-7">

                                <div class="table-responsive">
                                    <table id="customer-list" class="table">
                                        <!-- Loading Spinner Div -->
                                        <div id="loading-container">
                                            <p>Fetching all recyclable data...</p>
                                            <div id="loading-spinner">
                                                
                                            </div>
                                        </div>
                                        <tbody>
                                            <img id="myimage" />
                                        </tbody>
                                    </table>
                                </div>
                                
                                <!--AJAX call database-->
                    <script>
                        

                    
                        $.ajax({

                            url: "https://ecoexchange.dscloud.me:8080/api/get",

                            method: "GET",
                            // In this case, we are going to use headers as
                            headers: {
                                // The query you're planning to call
                                // i.e. <query> can be UserGet(0), RecyclableGet(0), etc.
                                query: "CustomerGet(0)",

                                // Gets the apikey from the sessionStorage
                                apikey: sessionStorage.getItem("apikey")
                            },



                            success: function (data, xhr, textStatus) {
                                console.log(data)
                            
                                
                                const buildTable = data => {
                                        const table = document.querySelector("#customer-list tbody");
                                        

                                        for (let i = 0; i < data.length; i++) {
                                            let row = 
                                            
                                            `
                                                    
                                                    <tr>
                                                    <td class="cell-recyclable-name"><img src = ${data[i].Picture}/></td>
                                                    <td class="cell-recyclable-name">${data[i].Name}</td>
                                                    </tr>`;
                                            
                                            table.insertAdjacentHTML('beforeEnd', row);
                                            //document.getElementById("myimage").src = `${data[i].Picture}`;
                                        }
                                    };

                                    // Fetch method
                                    const getData = async (url) => {

                                        const response = await fetch(url, {
                                            method: 'GET',
                                            headers: {
                                                query: "CustomerGet(0)",
                                                // Gets the apikey from the sessionStorage
                                                apikey: sessionStorage.getItem("apikey")
                                            }
                                        });
                                        const json = await response.json();
                                        $("#loading-container").hide();
                                        return buildTable(json);
                                    };
                                    getData('https://ecoexchange.dscloud.me:8080/api/get')
    
                        

                            },

                            error: function (xhr, textStatus, err) {
                                console.log(err);
                            }
                            });
                    </script>
                    </div>

i have no idea why image is not showing up instead is showing broken image link for now我不知道为什么图像没有显示,而是现在显示损坏的图像链接

Needed help for this issue as to why the image is not showing up需要有关此问题的帮助,以了解图像未显示的原因

NVM figure out reason why NVM 找出原因

<td class="cell-recyclable-name"><img src = "${data[i].Picture}"></img></td>

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

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