简体   繁体   English

如何使用Ajax将图像链接放入img标签

[英]How to put image link in img tag using ajax

I want to do is show the image to img tag using ajax and php. 我想做的是使用ajax和php将图像显示为img标签。

My problem is after receiving the link of the image from the emailName.php the image wont show up. 我的问题是从emailName.php接收到图像的链接后,图像将不会显示。

I check the emailName.php it sends this array to the ajax: 我检查emailName.php,它将这个数组发送到ajax:

{"user_lname":"Oppa","user_code":"1","user_image":"myDocuments\/images\/default.jpg"}

The user_lname and user_code are showing up but the image is not. 正在显示user_lname和user_code,但未显示图像。

I want to show the image on this tag. 我想在此标签上显示图像。

<img id="profileImage" name="profileImage" />
<label id="emailNameResult"></label>
<label id="emailCodeResult"></label>

script: 脚本:

$(document).ready(function(){



var countTimerEmailName = setInterval(
        function ()
        {
        emailName();        
        }, 500);


var  $emailNameResult = $('#emailNameResult');

function emailName(){
        $.ajax({
        url:"emailName.php",
        type:"GET",
        data: { term : $('#email').val() },
        dataType:"JSON",
        success: function(result) {

        $("#emailNameResult").html(result.user_lname);
        $("#emailCodeResult").html(result.user_code);
        $("#profileImage").html(result.user_image);

        }

    });

};

});

Use attr() to change the src 使用attr()更改src

$("#profileImage").attr('src', result.user_image);

attr() API docs attr()API文档

This assumes that the <img> tag shown already exists 假设显示的<img>标签已经存在

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

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