Thomas Fellner
Thomas Fellner

Reputation: 113

jQuery: Set image to data from Post request

I Would like to get the data from a post request an pass it onto an img tag. I know i could do it with an get request but i kinda need to use a post request to send the location of the file.

$.post('/getImage',{location: 'image.jpg'} ,function(image) {
    $('body').append('<img src=\'image\'></img>');
}

So is there any way to set the data from the post request to the img tag? The post request works just so you know so i don't need any help setting that up

Upvotes: 2

Views: 1139

Answers (2)

Alex Nikulin
Alex Nikulin

Reputation: 8689

what kind of data returns from server in your callback?

you have two ways:
1) Convert file at server to base64, and create Image tag at client and set base64 to src
2) Return at server binary data, and use at client native xhrRequest, like in this post 

https://stackoverflow.com/a/17682424/5138198

Upvotes: 1

Fa773N M0nK
Fa773N M0nK

Reputation: 120

Return the base64 encoding of the image and set the image's src attribute like this:

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

Upvotes: 1

Related Questions