Basic AJAX Question:
Download Questions PDF

How do I send an image using AJAX?


Answer #1
While it may appear that images are being sent when using AJAX with an application like Google Maps what is really happening is that the URLs of images are being send as the response of an AJAX request and those URLs are being set using DHTML.
In this example an XML document is returned from an AJAX interaction and the category bar is populated.

<description>Fun to read</description>
<description>Must have gadgets</description>

Notice that the image-url element contains the location of the URL for the image representing a category. The callback method of an AJAX interaction will parse the response XML document and call the addCategory function for each category included in the response XML document. The addCategory function looks up a table row element "categoryTable" in body of the page and adds a row to the element which contains the image.

Answer #2
<scrip type="text/javascript" >

function addCategory(id, name, imageSrc) {
var categoryTable = document.getElementById("categoryTable");
var row = document.createElement("tr");
var catCell = document.createElement("td");
var img = document.createElement("img");
img.src = ("images" + imageSrc);
var link = document.createElement("a");
link.className ="category";
link.setAttribute("onclick", "catalog?command=category&catid=" + id);
<td width="300" bgoclor="lightGray">
<table id="categoryTable" border="0" cellpadding="0"></table>
<td id="body" width="100%">Body Here</td>

Note that the source of the image is set to the image source. The image is loaded by a subsequent HTTP request for the image at the URL "images/books_icon.gif" or "images/electronic_icon.g

Download AJAX Interview Questions And Answers PDF

Previous QuestionNext Question
How do I provide internationalized AJAX interactions? How do I submit a form or a part of a form without a page refresh?