最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set image content from ajax response - Stack Overflow

programmeradmin1浏览0评论

I am making a ajax request in jquery as below. This ajax request gets images dynamically. And the image is always different image depending on the value of num

$.ajax({
    type: "POST",
    url: "ABC.php?num=1",
    success: function(response) {
          if(response == 1) {
                //some code
          }
          else {
                // Here I am not able to set image content.
                $("#image").attr("src", "data:image/png;base64,' + response + '");
          }
    }
});

Is there a way to set image content using the response of the ajax request. Thanks.

I am making a ajax request in jquery as below. This ajax request gets images dynamically. And the image is always different image depending on the value of num

$.ajax({
    type: "POST",
    url: "ABC.php?num=1",
    success: function(response) {
          if(response == 1) {
                //some code
          }
          else {
                // Here I am not able to set image content.
                $("#image").attr("src", "data:image/png;base64,' + response + '");
          }
    }
});

Is there a way to set image content using the response of the ajax request. Thanks.

Share asked Dec 9, 2013 at 11:02 AshwinAshwin 12.4k22 gold badges84 silver badges119 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You have some unneeded single quotes there. Use this:

$("#image").attr("src", "data:image/png;base64," + response);

Try this

$('#image').html('<img src="data:image/png;base64,' + response + '" />');

You need to send the image back base64 encoded, look at this: http://php/manual/en/function.base64-encode.php

I rarely use jquery but this worked for me: $(".avatar").html('<img src="' + user.avatar_url + '" />'); Here i am adding an image to a class and avatar_url es from the response from the server.

发布评论

评论列表(0)

  1. 暂无评论