<?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 BIZ - The Blog &#187; server connect with Ajax</title>
	<atom:link href="http://www.lostresort.biz/blog/tag/server-connect-with-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>Tue, 22 Nov 2011 08:15:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</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 is done using<br /><a class="read-rest" href="http://www.lostresort.biz/blog/2009/05/07/ajax-technology-fetching-data-from-server/">Read the rest ...</a>]]></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>
<pre class="crayon-plain-tag"><code>XMLHttpRequestObject = new XMLHttpRequest();</code></pre>
<p>In case that you are dealing with Internet Explorer the object can be created using code like this:</p>
<pre class="crayon-plain-tag"><code>XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);</code></pre>
<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>
<pre class="crayon-plain-tag"><code>if (window.XMLHttpRequest) {
   XMLHttpRequestObject = new XMLHttpRequest();
}</code></pre>
<p>For Internet Explorer we&#8217;ll have:</p>
<pre class="crayon-plain-tag"><code>if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
}</code></pre>
<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>
<pre class="crayon-plain-tag"><code>var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
   XMLHttpRequestObject = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
}</code></pre>
<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>
<pre class="crayon-plain-tag"><code>XMLHttpRequestObject.open(&quot;GET&quot;, dataSource);</code></pre>
<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>
<pre class="crayon-plain-tag"><code>XMLHttpRequestObject.onreadystatechange = function(){
if (XMLHttpRequestObject.readyState == 4 &amp;&amp; XMLHttpRequestObject.status == 200) 
{
   //here we'll place the necessary code to fetch the data}
}</code></pre>
<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>
<pre class="crayon-plain-tag"><code>documentGetElementById(divID).innerHTML = XMLHttpRequestObject.responseText;</code></pre>
<p>where the element with the <code>divID</code> id will be the placeholder for our requested data.</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>

