【问题标题】:AJAX returning link not imageAJAX返回链接不是图像
【发布时间】:2015-07-30 02:10:02
【问题描述】:

我得到的是链接而不是图片。

    $.ajax({
    type: "GET",
    url: "image.php",
    contentType: "image/png",
    success: function(result){
        $('.image').text(result);
    }
});

$image = "http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png";
echo '<img src="'.$image.'"></img>';

<div class="image"></div>

返回文本而不是图像:

<img src="http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png"></img>

【问题讨论】:

标签: php jquery html ajax


【解决方案1】:

替换

$('.image').text(result);

$('.image').html(result);

它会正常工作的。 text() 输出字符串,html() 输出 html 标记

【讨论】:

    【解决方案2】:

    试试:

    $.ajax({
        type: "GET",
        url: "image.php",
        contentType: "image/png",
        success: function(result){
            $('.image').html(result); //change text to html
        }
    });
    

    Text 用于将文本节点注入到元素中,它不会被解释为 html。 html() 方法将 html 写入元素并被解释为这样。

    【讨论】:

    • 我认为这是不言自明的。但是,好点@JayBlanchard。我会详细说明原因。
    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 2010-09-27
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多