How to get image with ajax request?
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 statist开发者_StackOverflowics, 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.
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');
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
http://www.anthonymclin.com/code/7-miscellaneous/98-on-demand-image-loading-with-jquery
You need to send the image in the right format to the AJAX request. Solutions are for example octect stream, base64 etc. there are many ways to achieve on demand content loading. The important thing is to keep server-side and client-side synchron.
It's like, if you send JSON to the server, get XML back, but expect HTML you're JS won't work.
精彩评论