Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding an image in an AJAX response

Is it possible (and supported cross-browser) to embed an image into the XML of an AJAX response, and then load that image using JavaScript?

I have a system that does some calculations based on the number of sessions running through it. The results are then graphed, and returned in two parts:

1) XML containing information about the graph, totals, and Image map data allowing the user to click on relevant areas.

2) The graph image.

As the data can change between the two requests (and could be expensive to calculate), I'd prefer to do it in a single request (return the image with the XML). The current implementation caches the stats for a small period so that the results of multiple requests will still match. As the amount of data that needs to be cached is going to be increasing (from ~2.5K to ~1.2MB), I would like to try an alternative method.

NOTE: I do not want to use inline b64 PNG images as they are not supported in IE.

like image 233
jthompson Avatar asked Mar 23 '09 17:03

jthompson


People also ask

What is response type in Ajax?

Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.

Which function is used to load a resource in the background in Ajax?

The browser performs a JavaScript call to the Ajax engine. In other words, create an XMLHttpRequest object. In the background, an HTTP request is made to the server and the appropriate data is retrieved. HTML, XML, or JavaScript data is returned to the Ajax engine which then delivers the requested data to the browser.


2 Answers

Can you not store the image on the server and send the URL to the client?

like image 52
schar Avatar answered Oct 04 '22 18:10

schar


As this seems like more work that it's worth, I've decided that a simpler solution would be:

1) Send XML data to the client with the details of what is to be graphed.

2) Client sends a request for the image, including the data to graph (similar to the Google Chart API).

This decouples the chart rendering from the data, and then it can be used in the future to generate generic charts for other data sets. The other benefit is that it doesn't require any caching server-side since only 1 request is used.

like image 34
jthompson Avatar answered Oct 04 '22 17:10

jthompson