简体   繁体   中英

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.

My problem is after receiving the link of the image from the emailName.php the image wont show up.

I check the emailName.php it sends this array to the 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.

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

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

attr() API docs

This assumes that the <img> tag shown already exists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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