<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lost Resort Blog &#187; AJAX</title>
	<atom:link href="http://www.lostresort.biz/blog/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lostresort.biz/blog</link>
	<description>Talking about us and about web site development</description>
	<lastBuildDate>Thu, 08 Jul 2010 11:34:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Ajax technology &#8211; fetching data from server</title>
		<link>http://www.lostresort.biz/blog/2009/05/07/ajax-technology-fetching-data-from-server/</link>
		<comments>http://www.lostresort.biz/blog/2009/05/07/ajax-technology-fetching-data-from-server/#comments</comments>
		<pubDate>Thu, 07 May 2009 08:25:38 +0000</pubDate>
		<dc:creator>Cristi_N</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ajax technology fetching data from server]]></category>
		<category><![CDATA[server connect with Ajax]]></category>
		<category><![CDATA[XMLHttpRequest object]]></category>

		<guid isPermaLink="false">http://www.lostresort.biz/blog/?p=431</guid>
		<description><![CDATA[Ajax stands for Asynchronous JavaScript and XML and is a set of techniques for creating highly interactive web sites and web applications. The desired result is to update the website pages, using data fetched from the Internet, without refreshing the web page in the users&#8217; browser. The communication between the server and the web browser [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Ajax</em></strong> stands for <em>Asynchronous JavaScript and XML</em> and is a set of techniques for creating highly interactive web sites and web applications. The desired result is to update the website pages, using data fetched from the Internet, without refreshing the web page in the users&#8217; browser.</p>
<p>The communication between the server and the web browser is done using <code>XMLHttpRequest</code> objects manipulated through a JavaScript function. The goal is to create a JavaScript function which, based on users action, will change the data displayed on users&#8217; browser following some well defined rules.</p>
<h3>Creating the XMLHttpRequest Object</h3>
<p>This object is built in the browser and, depending on what browser you are running the code, it is accessed in different way. Netscape Navigator (version 7.0 and later), Apple Safari (version 1.2 and later), and Firefox let you create <code>XMLHttpRequest</code> objects with code like this:</p>
<blockquote><p>
XMLHttpRequestObject = new XMLHttpRequest();
</p></blockquote>
<p>In case that you are dealing with Internet Explorer the object can be created using code like this:</p>
<blockquote><p>
XMLHttpRequestObject = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);
</p></blockquote>
<p>The object is accessed in those browsers through the <em>browser window object</em>, and checking it&#8217;s presence means that you are dealing with certain web browsers.</p>
<p>For Netscape, Safari and Firefox the code will be as follows:</p>
<blockquote><p>
if (window.XMLHttpRequest) {<br />
XMLHttpRequestObject = new XMLHttpRequest();<br />
}
</p></blockquote>
<p>For Internet Explorer we&#8217;ll have:</p>
<blockquote><p>
if (window.ActiveXObject) {<br />
XMLHttpRequestObject = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}
</p></blockquote>
<p>Although the <code>XMLHttpRequest</code> object supported in different browsers differs we can use the same basic properties and methods of this object.<br />
So, the code used to create XMLHttpRequest object for different web browsers will be:</p>
<blockquote><p>
var XMLHttpRequestObject = false;<br />
if (window.XMLHttpRequest) {<br />
XMLHttpRequestObject = new XMLHttpRequest();<br />
}<br />
else if (window.ActiveXObject) {<br />
XMLHttpRequestObject = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}
</p></blockquote>
<h3>Opening the XMLHttpRequest Object</h3>
<p>It’s time to open the <code>XMLHttpRequest</code> object. Here’s how you use the <code>XMLHttpRequest</code> object’s open method in general (items in square brackets are optional, and items in italics are placeholders that you fill in yourself):</p>
<blockquote><p>
open(<i>&#8220;method&#8221;</i>, <i>&#8220;URL&#8221;</i>[, <i>asyncFlag</i>[, <i>"userName"</i>[, <i>"password"</i>]]])
</p></blockquote>
<p>Where <code>method</code> is <em>the HTTP method used to open the connection, such as GET, POST, PUT, HEAD, or PROPFIND</em>, <code>URL</code> is <em>the requested URL</em>, <code>asyncFlag</code> is a <em>boolean value which indicates if the call is asynchronous (default value true)</em>, <code>userName</code> is <em>the user name of your account</em> and the <code>password</code> is <em>the password of your account</em>.</p>
<p>The method used to open the connection will be for this case the <code>GET</code> method. After the connection is open, we can start to open the <code>XMLHttpRequest</code> object and to configure it in preparation for connecting to the server and downloading data.</p>
<p>This piece of JavaScript code will prepare the object:</p>
<blockquote><p>
XMLHttpRequestObject.open(&#8220;GET&#8221;, dataSource);
</p></blockquote>
<p>Note that using the open method like this configures the <code>XMLHttpRequest</code> object and <strong>it does not connect, or open any connection to the server</strong>.</p>
<p>The <code>onreadystatechange</code> property <em>contains the name of the event handler that should be called when the value of the readyState property changes (read/write)</em>. Using this property we can call an anonymous function that will open the connection to the server and to download the necessary data. Inside the anonymous function, we need to check on the data that’s been downloaded: is the download complete and are we ready to use that data? We can determine that with two properties of the <code>XMLHttpRequest</code> object: <code>readyState</code> and <code>status</code>.</p>
<p>The value of <code>4</code> for the <code>readyState</code> property means that the download is complete and the value of <code>200</code> for the <code>status</code> property means that is completed OK. Checking these values with an <code>if statement</code> which returns the <code>true</code> value means that we can go further with our request.</p>
<p>The JavaScript code will be as follows:</p>
<blockquote><p>
XMLHttpRequestObject.onreadystatechange = function(){<br />
if (XMLHttpRequestObject.readyState == 4 &#038;&#038; XMLHttpRequestObject.status == 200)<br />
{//here we&#8217;ll place the necessary code to fetch the data}<br />
}
</p></blockquote>
<p>To recover the downloaded text we can use the <code>XMLHttpRequest</code> object’s <code>responseText</code> property. That property is where the <code>XMLHttpRequest</code> object stores downloaded plain text. If we use <code>XML</code> data the object property will be <code>responseXML</code> (we&#8217;ll use it a little bit later) which is common for all major browsers.</p>
<p>The JavaScript code will be:</p>
<blockquote><p>
documentGetElementById(divID).innerHTML = XMLHttpRequestObject.responseText;
</p></blockquote>
<p>where the element with the <code>divID</code> id will be the placeholder for our requested data.</p>
<h3>Connecting to the server</h3>
<p>In this example, we’re using the <code>GET</code> method, so we call the <code>XMLHttpRequest</code> object’s <code>send</code> method, passing it a value of <code>null</code>, which is JavaScript’s placeholder for a value of nothing.</p>
<p>The JavaScript code will be:</p>
<blockquote><p>
XMLHttpRequestObject.send(null);
</p></blockquote>
<p>At this point we have created the <code>XMLHttpRequest</code> object, we opened it and we have created the connection to the server. From now we can make useful <code>XML</code> to store data and to have the source for our Ajax requests.</p>
<h3>Using Ajax with XML</h3>
<p>In the Firefox/Mozilla brand of browsers, we have to specify that we’re going to be downloading <code>XML</code> data, not plain text. To do that, we have to set the <code>MIME type</code> of the download to <code>text/xml</code>. We can achieve this using this line of code:</p>
<blockquote><p>
XMLHttpRequestObject.overrideMimeType(&#8220;text/xml&#8221;);
</p></blockquote>
<p>Another difference will be that we will use the <code>XMLHttpRequest</code> object&#8217;s <code>responseXML</code> property, which holds a JavaScript XML document object:</p>
<blockquote><p>
var xmlDocument = XMLHttpRequestObject.responseXML;
</p></blockquote>
<p>Let&#8217;s say that we want to download data regarding <code>streets name</code> from a certain city area (Bucharest in my case). For this we have to create a variable <code>streetsName</code> which will hold the data parsed from the <code>XML</code> file ruled by the <code>Tag Name</code> read-only property:</p>
<blockquote><p>
streetsName = xmlDocument.getElementsByTagName(&#8220;streetName&#8221;);
</p></blockquote>
<p>After this we need to create the format for the placeholder of the data. In this case I will create a list of elements <code><</code>span<code>></code> displayed as <code>block</code>. Maybe it will be better to generate elements of an unordered list, but the problem is that, using the following sequence of JavaScript code, the HTML output will be a type of <code><</code>ul<code>></code><code><</code>/ul<code>></code><code><</code>li<code>></code><code>...</code><code><</code>/li<code>></code>, which means that we have an empty <code>ul</code> element and some orphans <code>li</code> elements, and this could lead to problems on CSS formatting. </p>
<p>The necessary code will be:</p>
<blockquote><p>
var xmlDocument = XMLHttpRequestObject.responseXML;<br />
streetsName = xmlDocument.getElementsByTagName("streetName");<br />
obj.innerHTML = "Here are the fetched streets name:";<br />
for (loopindex = 0; loopIndex < streetsName.length; loopIndex++)<br />
{<br />
obj.innerHTML += "<code><</code>span<code>></code>" + streetsName[loopIndex].firstChild.data + "<code><</code>/span<code>></code>";<br />
}
</p></blockquote>
<p>where <code>obj = document.getElementById(divID);</code> which is the container of our data.</p>
<p>The structure of the XML file will be simple:</p>
<blockquote><p>
<code><</code>?xml version="1.0" ?<code>></code><br />
<code><</code>streetsName></code><br />
<code><</code>streetName<code>></code>Calea Victoriei<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Regina Elisabeta<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Lascar Catargiu<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Iuliu Maniu<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Cotroceni<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Ion Ionescu de la Brad<code><</code>/streetName<code>></code><br />
<code><</code>streetName<code>></code>Calea Dorobantilor<code><</code>/streetName<code>></code><br />
<code><</code>/streetsName></code>
</p></blockquote>
<p>From now the only thing we need to create is a <code>form</code> element which, based on user <code>click</code> action, will execute the function that will fetch the desired data from server.</p>
<p>The entire code (HTML and JavaScript) can be downloaded from the following link, a <a href="http://www.lostresort.biz/examples/fetching-data-from-server-using-ajax-and-xml/fetch-data-using-ajax.html">simple example on how data is fetched form server using Ajax and XML</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lostresort.biz%2Fblog%2F2009%2F05%2F07%2Fajax-technology-fetching-data-from-server%2F&amp;linkname=Ajax%20technology%20%26%238211%3B%20fetching%20data%20from%20server"><img src="http://www.lostresort.biz/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.lostresort.biz/blog/2009/05/07/ajax-technology-fetching-data-from-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.970 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-26 13:33:57 -->
