【问题标题】:Want to show image , after getting image name using ajax, not want to return html想要显示图像,使用ajax获取图像名称后,不想返回html
【发布时间】:2014-11-20 09:57:25
【问题描述】:

我想在得到 ajax 的响应后显示图像

我有以下代码

<!DOCTYPE html>
<html>
  <title> Dashboard</title>
  <head>
 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        jQuery(document).ready(function() {
                url = "<?php echo $_GET['PATH_INFO']; ?>";
                $.ajax({
                         type: "POST",
                         url: 'http://localhost:8081/fetch_image',
                         dataType: "JSON",
                         data: {
                                external_source: url,

                        },
                        success: function(data) {
                                local_image= data[0]['local_source'];
                                alert(local_image);
                        }
                });

        });
    </script>
     </head>
<body>


</body>  

我不知道,如何在 ajax 请求成功时显示 local_image

我在 php 中需要这个,所以它会返回内容类型 jpeg 标题

【问题讨论】:

  • 只需设置图像元素的 src 属性并将其添加到 DOM,但我不确定您的意思:I need this in php && not want to return html???

标签: php jquery ajax


【解决方案1】:

在你的成功函数中添加:

$(body).append('<img src="' + local_image + '">');

【讨论】:

  • @HituBansal 这没有意义,你的意思是什么?
  • 我需要这样的东西..static01.nyt.com/images/2014/11/18/us/HEART/HEART-tmagSF-v4.jpg"; header('内容类型:图片/jpeg');回声文件获取内容($图像); ?>
  • @HituBansal 浏览器中的图像几乎总是通过HTML 显示。我猜你不想使用flash 或java applet 来显示图像,是吗?
  • @HituBansal 是的,就像 A. Wolff 所说:你不能用 javascript 生成 PHP 代码,因为 PHP 是服务器端,javascript 是客户端。跨度>
【解决方案2】:

添加一张空白图片。

<img id="local_source" style="display:none"/>

在AJAX成功函数中:

success: function(data) {
  $("#local_source").show().attr('local_source', data[0]['local_source']);
}

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2015-02-08
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多