Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image content from ajax response

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.

like image 307
Ashwin Avatar asked Dec 28 '25 23:12

Ashwin


1 Answers

You have some unneeded single quotes there. Use this:

$("#image").attr("src", "data:image/png;base64," + response);
like image 56
Halcyon Avatar answered Dec 31 '25 11:12

Halcyon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!