【问题标题】:To get image instead of url in AJAX call在 AJAX 调用中获取图像而不是 url
【发布时间】:2021-02-07 11:52:29
【问题描述】:

以下是我的html页面的相关部分,我想获取图像而不是图像地址(result[i].url)。另外我想知道这是否可能,因为我在这里使用了某些样式(CSS)。

function BindStudents() {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "http://127.0.0.1:8000/memes", success: function (result) {
                   
                    var totalCount = result.length;
                    var structureDiv = "";

                    for (let i = 0; i < totalCount; i++) {
                        structureDiv += "<tr>" +
                            "     <td>" + result[i].id + "</td>" +
                            "      <td>" + result[i].name + "</td>" +
                            "             <td>" + result[i].url + "</td>" +
                            "              <td>" +result[i].caption  + "</td>" +
                            "              <td><button class='btn btn-link' onclick='return confirm(\"Are you sure you want to delete this record?\",DeleteRow(" + result[i].id + "))'>Delete</button></td>" +
                            "           </tr>";
                    }
                     
                    $("#divBody").html(structureDiv);
              
                }
            });
        }

【问题讨论】:

    标签: javascript html css ajax


    【解决方案1】:

    如果服务器发送 URL,则服务器发送 URL。

    如果你想要一张图片,那么你需要:

    • 更改服务器端代码,使其发送图像(但 JSON 没有图像、文件或 blob 类型,因此您需要将其编码为文本……然后编写客户端代码来解码编码图片……然后找到某种显示方式……这可能涉及将其编码为data: URL)
    • 发送 Ajax 请求以从您提供的 URL 中获取图像

    当您创建所有其他 HTML 时,只创建一个 &lt;img&gt; 元素并将 src 设置为来自 JSON 的 URL 可能会简单得多。

    【讨论】:

    • 我会选择第二个选项,我不想更改服务器端的代码。对于基本的 html,我知道如何获取图像。我试过这个,但没有奏效。 ` `
    【解决方案2】:

    只需添加一个 HTML 元素 &lt;img&gt;:

    structureDiv += "<tr>" +
        "     <td>" + result[i].id + "</td>" +
        "      <td>" + result[i].name + "</td>" +
        "             <td><img src=\"" + result[i].url + "\"></td>" +
        "              <td>" +result[i].caption  + "</td>" +
        "              <td><button class='btn btn-link' onclick='return confirm(\"Are you sure you want to delete this record?\",DeleteRow(" + result[i].id + "))'>Delete</button></td>" +
        "           </tr>";
    

    【讨论】:

    • 这个可以,但是图片尺寸太大,如何调整高宽?我试过下面" &lt;td&gt;&lt;img src=\ width="300" height="400"" " + result[i].url + "\"&gt;&lt;/td&gt;" +
    • "&lt;td&gt;&lt;img src='" + result[i].url +"' width='300' height='400'"&gt;&lt;/td&gt;" +
    • 我在检查 html 时尝试了 " &lt;td&gt;&lt;img src=\"" + result[i].url + "width='300' height='400' \"&gt;&lt;/td&gt;" +,它被转换为 &lt;img src="https://images.pexels.com/photos/3573382/pexels-photo-3573382.jpeg width='300' height='400'"&gt;
    • 给它一个类并在css中调整大小。
    • 知道了! " &lt;td&gt; &lt;img src=\""+result[i].url+"\" width=\"300\" width=\"400\" &gt; &lt;/td&gt;" +
    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2018-12-14
    • 2018-10-04
    • 2017-12-09
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多