Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image with ajax request?

Tags:

jquery

ajax

php

There is some .php file, that returns .png image file.

How to get this image with jQuery $.ajax() ?

Also, is there were some mistakes with input parameters, .php file will return error in JSON way.

How to understand, which infomation was returned - image or JSON with error?

UPDATE:

I need to draw some statistics graphics with this script. .php file gets some data (for example account id, which statistics is requested) and checks - if this user (user id is got from session is allowed to view the requested user's statistics, or not). If it's allowed than image is returned, if not - than json error. So i can make but if there will be error, image doesn't load. But I need to show error notification to user.

like image 461
Larry Cinnabar Avatar asked Sep 24 '11 16:09

Larry Cinnabar


2 Answers

If you are return this image as attachment to response - I thing you can't get this attachment in javascript variable, otherwise if you return image in a response stream and set property content type you can just assign you .php to image srs:

$('img').attr('src', 'my-image-handler.php'); 
like image 116
Samich Avatar answered Sep 18 '22 05:09

Samich


You can use it like this:

<img src="data:image/png;base64,R0lGODlhUA... " width="80" height="15" />

So i suggest you get the base64 encoded string from the ajax request and use that to display the image

like image 27
Jan Dragsbaek Avatar answered Sep 18 '22 05:09

Jan Dragsbaek