简体   繁体   English

如何使用jquery ajax显示从数据库到图像src的图像文件路径

[英]how to display image filepath from database to image src using jquery ajax

i have a btn-edit when i click it modal window show's up then i use ajax to make request then display my request on the input tag of html.. 当我单击它时,我有一个btn-edit模态窗口显示出来,然后我使用ajax发出请求,然后在html的输入标签上显示我的请求。

$('#btn-edit_profile').click(function(){
 $('#modal-update_profile').modal({backdrop: 'static', keyboard: true});


    $.ajax({
            url:'../ajax/getprofile.php',
            type:'POST',
            data:{userid:user},
            dataType:'JSON',
            success: function(result){
            $('#profile_image').attr('src','data:image/png;base64,'+result.profile_image+'"/>');    
            $('#users_firstname').val(result.users_firstname);
            }
    });
}); 

but the problem i encountered here is that on the input tag it works fine but on the image src its not..for some reason i got this error on my console.. data:image/png;base64,../img/doc/ui-sam.jpg net::ERR_INVALID_URL 但是我在这里遇到的问题是,在输入标签上它可以正常工作,但在图像src上却不能..由于某种原因,我在控制台上收到此错误。data data:image/png;base64,../img/doc/ui-sam.jpg net::ERR_INVALID_URL

what is the correct approach to display the filepath to src?? 什么是显示src的文件路径的正确方法?

the file path of on my image from my database is ../img/doc/ui-sam.jpg and the return JSON profile_image: "../assets/img/doctors/ui-sam.jpg" 我数据库中图片的文件路径为../img/doc/ui-sam.jpg ,返回JSON profile_image: "../assets/img/doctors/ui-sam.jpg"

and this is where i want the image from my db to be displayed <img class="img-responsive" id="profile_image" name="profile_image" src=""/> 这是我希望显示数据库中的图像的地方<img class="img-responsive" id="profile_image" name="profile_image" src=""/>

$('#btn-edit_profile').click(function(){
 $('#modal-update_profile').modal({backdrop: 'static', keyboard: true});


    $.ajax({
            url:'../ajax/getprofile.php',
            type:'POST',
            data:{userid:user},
            dataType:'JSON',
            success: function(result){
            $('#profile_image').attr('src',result.profile_image);    
            $('#users_firstname').val(result.users_firstname);
            }
    });
}); 

You're returning a URL for the image, that is all the src attribute needs. 您将返回图像的URL,这就是src属性所需的全部。

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

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