<?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>JavaScript Workshop &#187; Ajax</title>
	<atom:link href="http://www.javascriptworkshop.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptworkshop.com</link>
	<description>Helping others learn JavaScript and web development in general.</description>
	<lastBuildDate>Tue, 06 Sep 2011 11:10:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Beginning ASP.NET AJAX Web Development Class</title>
		<link>http://www.javascriptworkshop.com/2009/08/16/beginning-asp-net-ajax-web-development-class/</link>
		<comments>http://www.javascriptworkshop.com/2009/08/16/beginning-asp-net-ajax-web-development-class/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 13:24:38 +0000</pubDate>
		<dc:creator>Frank Stepanski</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Closed Online classes]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[LVSOnline]]></category>
		<category><![CDATA[Online classes]]></category>

		<guid isPermaLink="false">http://www.javascriptworkshop.com/?p=754</guid>
		<description><![CDATA[This class has been closed, and it&#8217;s course materials have been moved to the new Database ASP.NET Web Development class I created an AJAX ASP.NET class called AJAX Web Development with ASP.NET at LVSOnline.com. This fourth ASP.NET class in the series focuses on teaching students how the Ajax can be integrated within the ASP.NET framework. [...]]]></description>
			<content:encoded><![CDATA[<div id="update">This class has been closed, and it&#8217;s course materials have been moved to the new Database ASP.NET Web Development class</div>
<p><br/></p>
<p><del datetime="2011-04-06T17:53:19+00:00">I created an AJAX ASP.NET class called <a target="_blank" href="http://www.lvsassociates.com/register/product_info.php?products_id=295">AJAX Web Development with ASP.NET</a> at <a target="_blank" href="http://www.lvsonline.com">LVSOnline.com</a>.  This fourth ASP.NET class in the series focuses on teaching students how the Ajax can be integrated within the ASP.NET framework.</p>
<p></del><del datetime="2011-04-06T17:53:52+00:00"><a target="_blank" href="http://en.wikipedia.org/wiki/Ajax_(programming)">Ajax</a> (Asynchronous JavaScript and XML) isn&#8217;t a technology but rather a grouping of technologies. Ajax uses a communication technology (typically SOAP and XML) to send and receive an asynchronous request/response to the server, and then leverages presentation technologies (JavaScript, DOM, HTML, and CSS) to process the response.</p>
<p></del><del datetime="2011-04-06T17:53:52+00:00">Ajax lets you execute a server-side method through a JavaScript call, without requiring a browser refresh.</p>
<p></del><del datetime="2011-04-06T17:53:52+00:00">The class is only $30 and there are no other materials or software needed to purchase.</p>
<p></del><del datetime="2011-04-06T17:53:52+00:00"><strong>Topics covered:</strong></p>
<p></del><del datetime="2011-04-06T17:53:52+00:00">Week 1: Ajax overview; partial rendering; Ajax Extension controls; Script Manager<br />
Week 2: Update Panel; Handing client errors; Update Progress<br />
Week 3: AJAX Control Toolkit<br />
Week 4: JavaScript libraries: jQuery<br />
Week 5: Timer, Web Services and JSON Overview<br />
Week 6: AJAX ASP.NET Controls: Telerik<br />
</del><br />
<del datetime="2011-04-06T17:53:52+00:00">To give you an idea of how each lesson looks, here is the <a target="_blank" href = "http://www.javascriptworkshop.com/wp-content/uploads/pdf/LVSOnline_AJAXASPNET_wk1.pdf"> first lesson</a> (.pdf).</p>
<p></del><del datetime="2011-04-06T17:53:52+00:00"><strong>When Does Class Start?</strong></p>
<p></del><del datetime="2011-04-06T17:53:52+00:00">The class runs five times a year for six weeks in January, March, May, August and October.  To learn more view the <a target="_blank"  href="http://www.lvsassociates.com/register/product_info.php?products_id=295">course description page </a>and <a href="http://www.javascriptworkshop.com/contact-me/">contact me</a> to find out the exact date on when you can register.<br />
</del></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptworkshop.com/2009/08/16/beginning-asp-net-ajax-web-development-class/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AJAX 101 – part 2</title>
		<link>http://www.javascriptworkshop.com/2009/08/07/ajax-101-%e2%80%93-part-2/</link>
		<comments>http://www.javascriptworkshop.com/2009/08/07/ajax-101-%e2%80%93-part-2/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 14:11:43 +0000</pubDate>
		<dc:creator>Frank Stepanski</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[AJAX Basics]]></category>
		<category><![CDATA[AJAX Engine]]></category>

		<guid isPermaLink="false">http://www.javascriptworkshop.com/?p=730</guid>
		<description><![CDATA[Creating an object-oriented AJAX engine Once there is an understanding of the AJAX engine the focus can be on the actual response and page display; this is where the real power lies. Since rewriting the engine for each project is tedious and time consuming, an object-oriented solution can be very useful. This kind of engine [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Creating an object-oriented AJAX engine</strong></p>
<p>Once there is an understanding of the AJAX engine the focus can be on the actual response and page display; this is where the real power lies. Since rewriting the engine for each project is tedious and time consuming, an object-oriented solution can be very useful. This kind of engine can be reused for all of your AJAX projects and scaled to meet different projects’ needs. </p>
<p>An object-oriented AJAX engine will also help you separate your interface code from your request data, which will enable you to reuse this engine in multiple projects. The AJAX engine is not difficult to construct once you understand the syntax for creating custom JavaScript objects. The first step is to create a constructor function, which will be the access point to the object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Ajax&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-730"></span></p>
<p>This function will become the custom object and will therefore include all of the properties and functions that a standard AJAX engine has. </p>
<p>Here is an example of the engine in its entirely:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Ajax&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">makeRequest</span> <span style="color: #339933;">=</span>  <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>_method<span style="color: #339933;">,</span> _url<span style="color: #339933;">,</span> _callbackMethod_<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">new</span>
XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;MSXML2.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span>.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> _callbackMethod<span style="color: #339933;">;</span>
	     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>_method<span style="color: #339933;">,</span> _url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checkReadyState</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>_id<span style="color: #339933;">,</span> _1<span style="color: #339933;">,</span> _2<span style="color: #339933;">,</span> _3<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	     swtich<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span>.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span>
	     <span style="color: #009900;">&#123;</span>
	           case1<span style="color: #339933;">:</span>
                            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>_id_.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> _1<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                   case2<span style="color: #339933;">:</span>
                            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>_id_.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> _2<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                   case3<span style="color: #339933;">:</span>
                            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>_id_.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> _3<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>                 
                   case4<span style="color: #339933;">:</span>
                            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">request</span>.<span style="color: #000066;">status</span><span style="color: #339933;">;</span>
	      <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But in comparison, the main differences between this and the standard AJAX engine is the syntax in which it’s written, the way in which it is used and the speed with which it executes larger amounts of data. The following code is an example of how to use the AJAX object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> ajax <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ajax.<span style="color: #660066;">makeRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Another major change is the response handling, which gives us more control over the loading message and changes targeting of the AJAX engine’s methods. Here is an example of the entire request.js file for the XML request that we used in the standard engine using the new object-oriented engine:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> ajax <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> onResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ajax.<span style="color: #660066;">checkReadyState</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'loading...'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'loading...'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'loading...'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;OK&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	     <span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> ajax.<span style="color: #660066;">request</span>.<span style="color: #660066;">responseXML</span>.<span style="color: #660066;">documentElement</span><span style="color: #339933;">;</span>
             <span style="color: #003366; font-weight: bold;">var</span> header <span style="color: #339933;">=</span> response.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'header'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
     	     <span style="color: #003366; font-weight: bold;">var</span> header <span style="color: #339933;">=</span> response.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'copy'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstchild</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
&nbsp;
  	     document.<span style="color: #660066;">GetElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;b&gt; + header + &quot;</span><span style="color: #339933;">&lt;/</span>b<span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot; + copy;
	}
}</span></pre></div></div>

<p>The code is cleaner and the separation is much easier to understand. Although the standard AJAX engine works fine for making requests, it can become cumbersome to maintain. With the object-oriented engine we can begin to add more power to our applications. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptworkshop.com/2009/08/07/ajax-101-%e2%80%93-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Magazine &#8211; JsMag</title>
		<link>http://www.javascriptworkshop.com/2009/07/27/javascript-magazine-jsmag/</link>
		<comments>http://www.javascriptworkshop.com/2009/07/27/javascript-magazine-jsmag/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:05:02 +0000</pubDate>
		<dc:creator>Frank Stepanski</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript topics]]></category>
		<category><![CDATA[JavaScript magazine]]></category>
		<category><![CDATA[JsMag]]></category>

		<guid isPermaLink="false">http://www.javascriptworkshop.com/?p=718</guid>
		<description><![CDATA[Even with all the information on the web today, it can be difficult to find current information on JavaScript sometimes. There are a few good web magazines out there, but none truly focus solely on JavaScript related topics. I recently found an online magazine called JsMag. The magazine has some great information on topics such [...]]]></description>
			<content:encoded><![CDATA[<p>Even with all the information on the web today, it can be difficult to find current information on JavaScript sometimes. There are a few good web magazines out there, but none truly focus solely on JavaScript related topics. I recently found an online magazine called <a target = "_blank" href="http://www.jsmag.com/">JsMag</a>.</p>
<p><a target="_blank" href="http://www.jsmag.com/"><img id="jsmag" align="right" src="http://www.javascriptworkshop.com/jsmag.jpg" alt="JsMag" /></a>The magazine has some great information on topics such as Ajax, code optimization, mashups, jQuery, web application testing, event handling, EXT JS, OOP, and much more. Its unique content, focusing on mainly JavaScript related topics, keeps me continously learning new things in the JavaScript world. Talented developers write different articles each month so content is always new and comes from different perspectives.</p>
<p>The monthly issues (.pdf) are only $4.99 or you can purchase a years subscription as well. It definitely is worth the small investment if you are a JavaScript developer or interested in learning more intermediate to advanced JavaScript topics. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptworkshop.com/2009/07/27/javascript-magazine-jsmag/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AJAX 101 &#8211; part 1</title>
		<link>http://www.javascriptworkshop.com/2009/06/21/ajax-101-part-1/</link>
		<comments>http://www.javascriptworkshop.com/2009/06/21/ajax-101-part-1/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 18:43:01 +0000</pubDate>
		<dc:creator>Frank Stepanski</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[AJAX Basics]]></category>
		<category><![CDATA[AJAX Engine]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XMLHTTPRequest]]></category>

		<guid isPermaLink="false">http://www.javascriptworkshop.com/?p=634</guid>
		<description><![CDATA[AJAX, an acronym for Asynchronous JavaScript and XML, is a group of technologies combines to accomplish powerful new forms of the web interactions. It&#8217;s composed of JavaScript, the XMLHTTPRequest object and XML, JSON, (X)HTML or even plain text. Asynchronous means that you can make an HTTP request to a server and continue to process other [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://en.wikipedia.org/wiki/Ajax_(programming)">AJAX</a>, an acronym for Asynchronous JavaScript and XML, is a group of technologies combines to accomplish powerful new forms of the web interactions. It&#8217;s composed of JavaScript, the XMLHTTPRequest object and XML, JSON, (X)HTML or even plain text. </p>
<p>Asynchronous means that you can make an HTTP request to a server and continue to process other data while waiting for the server’s response. For example, you can make requests to a server-side script to retrieve data from a database as XML, send data to a server-side script to be stored in a database or simple load a static XML file to populate pages of your web site without ever refreshing the page.</p>
<p>Behind the scenes data transfers are accomplished through the <a target="_blank" href="https://developer.mozilla.org/en/XmlHttpRequest">XMLHTTPRequest object</a>, which is the heart of the AJAX engine, as it&#8217;s used to exchange data asynchronously with a server-side language, XML or other text format. It combines with the DOM (Document Object Model) to display the AJAX response data as (X)HTML and CSS. </p>
<p>The DOM is a language-independent interface that makes common web page elements accessible through scripting languages. This is extremely useful when working with dynamic data, such as an AJAX response, because it can be parsed by JavsScript and added to page elements on the fly without a browser refresh.<br />
<span id="more-634"></span></p>
<p>In 1999, Microsoft brought the XMLHTTPRequest object to the web with the release of IE 5.0 as an ActiveX object that was available through JavaScript and VBScript. Since its creation, Mozilla and Apple and Opera have included it in their browsers as well (as a native object).</p>
<p>AJAX is a valuable connection between the interface and backend logic, enabling the backend to be robust and powerful with a simpler, yet intuitive interface that provides as on demand feedback to users. It also provides ways to exchange data with server-side languages and to store it in databases without disconnecting the user from the application, such as standard applications do when refreshing the browser window. </p>
<p><strong>The AJAX Engine</strong></p>
<p>The standard AJAX engine is based on the technologies that make up the acronym: the asynchronous functionality sends requests to a server, which are a type of two-way time delayed communication, enabling data to respond when it’s ready; JavaScript, which creates the object and makes the request and XML, could actually be XML, JSON (<a target="_blank" href="http://www.json.org/">JavaScript Object Notation</a>) or any other text format that the server returns as a response to the request. </p>
<p>The data that is returned could be a static file that’s updated by hand or can be a server-side script that returns database data in XML, JSON or another text format. </p>
<p>The standard AJAX engine is fairly simple and easy to learn, especially with prior knowledge of JavaScript and XML. The easiest way to create the AJAX engine is by adding a separate JavaScript file to your project and pointing to it from your (X)HTML. </p>
<p>This file will include all the methods (or functions) that make up this request. Here is how you make the request from your (X)HTML page.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span> Introduction to Ajax <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/request.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">onload</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript :makeRequest (‘path to…/filename’);&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;body&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></div></div>

<p>Notice that we&#8217;re pointing to the JavaScript source, called request.js in the script tag. This file will include the AJAX code.</p>
<p>When the page loads, you need to pass the path of the file that you’re requesting to a method that handles the request. This method, named makeRequest, will receive the path to the requested file and create the XMLHTTPRequest object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// IE7, IE8, Mozilla, Safari, etc.</span>
	request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// IE 6 and previous</span>
	<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
               request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
sendRequest <span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Internet Explorer 6 and before use the ActiveX object, while the more current Internet Explorer versions 7 and 8, as well as Firefox, Opera, and Safari all use the XMLHttpRequest object.</p>
<p>The URL parameter is the path that we passed to the method when the page loaded, this parameter is passed to the sendRequest method to complete the request:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> sendRequest <span style="color: #009900;">&#40;</span>ur<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	request.<span style="color: #660066;">obreadystatechange</span> <span style="color: #339933;">=</span> onResponse<span style="color: #339933;">;</span>
	request.<span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The sendRequest method uses the request object that was created in the previous method and triggers three additional method calls which are intrinsic to the request object. The first is the <a  target="_blank" href="http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp">onreadystatechange</a>, which sets the callback method that we’ll create to handle the response of the request. </p>
<p>The second is the <a target="_blank" href="http://www.w3schools.com/ajax/ajax_server.asp">open</a> method, which included the HTTP request method, the URL or path to the file that we want to request and a Boolean (a true/false value) defining whethr the call is asynchronous, with true meaning asynchronous. </p>
<p>Lastly, the <a target="_blank" href="http://www.w3schools.com/ajax/ajax_server.asp">send</a> method takes additional parameters that can be sent as a query string to a server-side script.</p>
<p>Once the request has been made and the server returns a response, we need to check the readyState of the object to determine if the response has been completely loaded. The readyState included four states: zero is Uninitialised, one is Loading, two is Loaded, three is Interactive and four is Complete.</p>
<p>When the response is fully loaded the HTTP status code will equal 200, which means that the response data can then be accessed successfully. Below is an example of the checkReadyState method, which handles the different states:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> checkReadyState <span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// uninitialized</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// loading</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// loaded</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// interactive</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// complete</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// successfully loaded return true:</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;There was a problem retrieving the XML.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, this method handles all the ready states and has the ability to display a custom message for each state. Once the state equals four, meaning that it has been fully loaded, the request status is checked. If it equals 200, the object has been successfully loaded and we can return to tell the response handler to process the data. If no custom error message is displayed. </p>
<p>This example only displays one message for all errors, but there are many HTTP states available, which are outlined by the World Wide Web Consortium and can be found on its web site (<a  target="_blank" href="http://www.w3.org/">www.w3c.org</a>).</p>
<p>Once the response data has been successfully loaded it’s ready to be parsed. There are two intrinsic methods for retrieving the response data, called &#8216;responseText&#8217; and &#8216;response XML&#8217;. The responseText method handles all text formats and can be used as a way to debug the response data. Here&#8217;s an example for loading HTML data:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">b</span>&gt;</span> This is the header <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">b</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
This is the body</pre></div></div>

<p>This is a simple sample, but it shows that the HTML tags can be used in the response file and that they’ll render properly in requests. Using responseText to retrieve the data is as easy as using the following syntax in the callback method that we set in the (X)HTML page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> onResponse <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>checkReadyState<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> request.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> checkReadyState<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method displays the data or error message as a HTML tag. This works well when displaying plain text or (X)HTML, but if the request is JSON we need to evaluate the response and parse its properties. Here’s an example of the JSON data format:</p>
<p>{&#8216;header&#8217; : &#8216;This is the header&#8217;, &#8216;body&#8217;: &#8216;This is the body&#8217; }</p>
<p>The data is separated into two sections, the name and the value, by a colon. Additional data is separated by commas into new name/value pairs. Now that we know what the JSON format looks like, this is how we parse it as a response:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> onResponse <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>checkReadyState<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;var response = (&quot;</span> <span style="color: #339933;">+</span>request.<span style="color: #660066;">responseText</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     document.<span style="color: #660066;">getElementByID</span><span style="color: #009900;">&#40;</span>‘body’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;br&gt;&quot;</span> <span style="color: #339933;">+</span> response.<span style="color: #660066;">header</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/b&gt;&lt;br/&gt;&quot;</span>
&nbsp;
     response.<span style="color: #660066;">body</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first thing to do when we receive the JSON data is evaluate and give it a name, this creates a JavaScript object from the data by the name that we gave it. Once we have the object created, we can call the properties of the object by name. for example, response.header gives us the value of the header.</p>
<p>The responseText is useful for all text formats, but when we want to parse XML we need to use the reponseXML method, which returns an XML object from XML response data. This object can be accessed by first targeting its root element with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> request.<span style="color: #660066;">responseXML</span>.<span style="color: #660066;">documentElement</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once the root has been targeted properly the elements in the XML can be targeted by name and displayed in the page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> header <span style="color: #339933;">=</span> response.<span style="color: #660066;">getElementByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'header'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'copy'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> header<span style="color: #339933;">;</span></pre></div></div>

<p>The standard AJAX engine is simple to create once you understand the different parts of the process. The only downfall is that you can have different versions of the request in multiple projects and this can become a hassle. </p>
<p>An object-oriented solution helps to avoid issues regarding redundant code, and will provide a more powerful engine. This will be dicussed in part 2 of this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptworkshop.com/2009/06/21/ajax-101-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

