【问题标题】:Why I cannot insert a image in html after I fetch the image url为什么我在获取图像 url 后无法在 html 中插入图像
【发布时间】:2017-09-26 01:29:53
【问题描述】:

我获取图像 URL,然后将此 URL 插入图像的 src,但它没有显示任何图像?我在这里遵循了这个想法(https://stackoverflow.com/a/12784213/8229192

这是我的代码:

HTML:

<h1>Gif image search</h1>
<div id="problemform" class="form-inline">
    <input id="probleminput" class="form-inline" placeholder="Enter your keyword" type="text" style="display: inline;"></input>
    <button id="problemsubmit" class="btn" style="display: inline-block;">Submit</button>
</div>

<div>Showing <h2 id="resultCount">0</h2>results<div>
<img src="" id="picture"/>

js代码:

$('#problemsubmit').on('click', function(event) {
    event.preventDefault();
    var formInput = $('#probleminput').val();

    var xhr = $.get("http://api.giphy.com/v1/gifs/search?q=" + formInput + "&api_key=o1H3Da15oqhgM3WlAhPnQYJV8g9l3NdV&limit=6");
    xhr.done(function(data) {
            console.log("success got data", data);
            $('#resultCount').html(data.data.length);
            var imageString = data.data[0].url + "";
            $('#picture').attr('src', imageString);
        }
    );
});

【问题讨论】:

  • 为什么要投反对票?请解释
  • 您的 jsfiddle 不包含任何与您的 sn-p 相关的内容。
  • 您的代码应发布在此处。不是链接。
  • 正如您的名字所说,您可能是入门级开发人员,但请在此处粘贴您的代码,然后链接,以便我们理解
  • @lilezek 抱歉,我附加了错误的链接。我修好了

标签: javascript html


【解决方案1】:

您在 Giphy API 中使用的 url 参数链接到他们网站上包含该图像的页面。该参数并不意味着嵌入。

您需要深入研究数据以找到可以实际嵌入为图像的 GIF URL。您需要从数组中选择一个大小并使用images.[size].url,而不是使用url

在 Giphy 的 API 文档中了解图像对象:
https://developers.giphy.com/docs/#images-object

这是一个使用 original 大小的 GIF 的实时示例:

$('#problemsubmit').on('click', function(event) {
  event.preventDefault();
  var formInput = $('#probleminput').val();

  var xhr = $.get("https://api.giphy.com/v1/gifs/search?q=" + formInput + "&api_key=o1H3Da15oqhgM3WlAhPnQYJV8g9l3NdV&limit=6");
  xhr.done(function(data) {
    $('#resultCount').html(data.data.length);
    var imageString = data.data[0].images.original.url;
    $('#picture').attr('src', imageString);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Gif image search</h1>
<div id="problemform" class="form-inline">
  <input id="probleminput" class="form-inline" placeholder="Enter your keyword" type="text" style="display: inline;" value="hello"></input>
  <button id="problemsubmit" class="btn" style="display: inline-block;">Submit</button>
</div>

<div>Showing
  <h2 id="resultCount">0</h2>results
  <div>
    <img src="" id="picture" />
  </div>
</div>

【讨论】:

  • 谢谢。你怎么知道我需要做“data.data[0].images.original.url”而不是“data.data[0].url”?
  • @EntryLeveDeveloper 这一切都在 Giphy API 中进行了解释,我建议您阅读以了解您想要使用的尺寸以及其他一些功能(例如随机化图像)。当我访问浏览器中的链接并看到它没有返回 GIF 时,我知道 data.url 不起作用。然后我查看了 AJAX 请求返回的完整数据,并看到了图像 URL 数组。
  • 这是data.data[0].url:giphy.com/gifs/studiosoriginals-l0IypqLJbGblf4vDy。这是 data.data[0].images.original.url: media3.giphy.com/media/l0IypqLJbGblf4vDy/giphy.gif 如果我在浏览器中打开它,我可以看到这两个图像。请指教。谢谢。
  • 我会再次让您使用 API。前者 (url) 是“此 GIF 的唯一 URL” - 旨在在线共享,让人们点击 Giphy.com 以查看有关图像的信息并共享图像。后者 (images.[size].url) 是“此 GIF 的可公开访问的直接 URL”。您需要图像的直接 URL 才能在 HTML 图像对象中使用它 - 而不是 Giphy 页面 about 图像的链接。
猜你喜欢
  • 2021-10-01
  • 2019-11-27
  • 2013-12-22
  • 2017-06-21
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多