简体   繁体   English

如何显示来自ajax成功函数的图像?

[英]How to display the image from ajax success function?

I have created ajax function like this...In this I will get the value from run time and i need to return the photo according to that value..In success function i need to display that image in particulat div 我已经创建了像这样的ajax函数...在这种情况下,我将从运行时获取值,并且我需要根据该值返回照片。在成功函数中,我需要在particulat div中显示该图像

var num=document.getElementById('number').value;
$.ajax({
    url:"image.php?val="+num,
    contentType: "image/png",
    success:function(img)
    {
        $('#image').html('<img src="data:image/png;base64,' + img + '" />');
    }
    });

image.php page image.php页面

$sql_sub = select_query("select pic from  photo  where picnum=".$_GET['val']."");
$img = $sql_sub[0][0]->load();
header("Content-type: image/png");
ob_start();
imagepng($img);
echo "data:image/png;base64,", base64_encode(ob_get_clean());

It looks perfect..You may have an issue in tag. 看起来很完美。。您的标签可能有问题。 Check first that tag. 首先检查该标签。 However .append works great. 但是.append效果很好。

Have you tried this: 您是否尝试过:

$('body').append('<img src="https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.com" />');

$('#div_where_you_will_sho_qr_code').append(data.toString());

or: 要么:

$('#container').html('<img src="https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.com" />');

where #container is some DOM element to harbor your image. #container是一些DOM元素来保存您的图像。

or the way I prefer: 或我更喜欢的方式:

$('#container').html(
    $('<img/>', {
        src: 'https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.com',
        alt: ''
    })
);
var num=document.getElementById('number').value;
$.ajax({
    url:"image.php?val="+num,
     type: "POST",
     dataType: "html",
    success:function(data)
    {
        $('#image').html(data));
    }
    });

image.php image.php

$sql_sub = select_query("select pic from  photo  where picnum=".$_POST'val']."");
$img    = $sql_sub[0][0]->load();
$image  = '<img src="data:image/png;base64,'.$img.'" />';
echo $img;
var num=document.getElementById('number').value;
$.ajax({
url:"image.php?val="+num,
 type: "POST",
 dataType: "html",
success:function(img)
{
    $('#image').html('<img src="data:image/png;base64,' + img  + '" />');
}
});

image.php image.php

$sql_sub = select_query("select pic from photo where picnum=".$_GET['val']."");
$img = $sql_sub[0][0]->load();
header("Content-type: image/png");
ob_start();
echo $img;
echo "data:image/png;base64,", base64_encode(ob_get_clean());

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

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