In “AJAX Fast and Easy - part one“, we defined AJAX. We said the Asynchronous part could be compared to opening a link in a new window so that you don’t leave your current page. We said the Javascript part could be as simple as adding a call to the freely available Prototype Javascript framework. We decided that XML was pretty simple to understand because all it does is describe the data you put in it, and we even went as far as saying that XML is not necessary when writing AJAX.

The first thing we will do in part two is prove that last statement. XML is not necessary to write AJAX. So, without further ado, take out your notepads, or your favorite HTML editor and get ready to write some AJAX code. If you are running a web server, great, but if not, don’t worry about it. If you have Firefox installed, you can run all of this code without a web server.

Lets go about this step by step, so we can see how easy it really is.

  • Create a new directory for our files, and name it ajaxHowTo (ex. C:\ajaxHowTo\).
  • Create a new file in C:\ajaxHowTo\ named index.html.
  • Open index.html for editing and enter the code below to create a simple web page.
<html>
  <head>
    <title>Ajax How To</title>
    <script src="prototype-1.6.0.2.js" type="text/javascript"></script>
     </head>
  <body>
	<p>
		<a href="javascript: void(0);" onclick="getSampleHTML();">call getSampleHTML()</a> 
		and display results in alert from sample code
	</p>
	<p>
		<a href="javascript: void(0);" onclick="getHTML();">call getHTML()</a> 
		and display results in a div below 
		<a href="javascript: void(0);" onclick="clearNode('htmlResult');">clear</a>
	</p>
	<p>
		<a href="javascript: void(0);" onclick="getXML();">call getXML()</a> 
		and display results in a div below 
		<a href="javascript: void(0);" onclick="clearNode('xmlResult');">clear</a>
	</p>
	<p>Display the AJAX results below:</p>
	<div id="htmlResult">
	</div>
	<div id="xmlResult">
	</div>  
  </body>
</html>
  • Download the Prototype Javascript framework and save it to the ajaxHowTo folder (ex. C:\ajaxHowTo\prototype-1.6.0.2.js)
  • Feel free to open up your index.html file and view it in a web browser. The links won’t do much right now because we haven’t written the javascript functions yet, but it should look like this. For the very lazy, I have packed all the sample code in a zip file for download.

  • Inside the head section of the html file, create another script section so it looks like this.
  • <html>
      <head>
        <title>Ajax How To</title>
        <script src="prototype-1.6.0.2.js" type="text/javascript"></script>
    	<script>
    
    	</script>
         </head>
      <body>
    
  • Inside of the new script block, we will now create our first Javascript function and name it getSampleHTML(). This function is almost an exact copy of the sample from the prototype examples found at www.prototypejs.org/learn/introduction-to-ajax. The only difference is in the html file we call in the Ajax.Request section.
  • function getSampleHTML() {
    	new Ajax.Request('backendHTML.html',   {     
    		method:'get',     
    		onSuccess: function(transport){       
    			var response = transport.responseText || "no response text";       
    			alert("Success! \n\n" + response);     
    		},     
    		onFailure: function(){ 
    			alert('Something went wrong...') 
    		}   
    	}); 
    }
    
  • Just like steps 2 and 3, create a new html file and name it backendHTML.html. Make it look like the code block below and save it to the ajaxHowTo folder.
  • <html>
    	<head>
    		<title>Ajax How To</title>
    	</head>
    	<body>
    		Hey, you just wrote some AJAX!!
    		<br/>
    		Wasn't that EASY!!!!
    	</body> 
    </html>
    

    We should now have 3 files inside of our new folder /ajaxHowTo/. We should have index.html, backendHTML.html, and prototype-1.6.0.2.js.

    That is all we need. We just wrote our first AJAX code! Wasn’t that easy? Just two HTML files and one javascript function!

    Now, go ahead and open index.html. Click on the first link labeled “call getSampleHTML()”. Clicking that link causes a call to our Javascript function named getSampleHTML(). That function calls a function built into Prototype called Ajax.Request(). Ajax.Request() gets passed the name of the file we want to call via AJAX, in this case “backendHTML.html”. In the onSuccess section of our function, we specified that we want an alert to appear with the words “Success!” followed by the content of the file we called through AJAX.

    Thats all there is to it!


    It’s as simple as 1,2,3!


    Page A makes an AJAX call to page B, and page A displays the content of page B.

    Thats AJAX!

    That concludes “Ajax Fast and Easy - part two”. In part three, we will expand on these files by adding the ability to render the AJAX response on the current page as HTML, and we will make a call to an actual XML file, parse the XML and render the XML response on the page as well. Don’t worry, I promise it will be fast and easy.

    UPDATE:


    Ajax Fast and Easy - part three is now available

     



    Tags: , , , , , , , , ,

    Trackback URL | Comments RSS

    10 Responses to “Ajax Fast and Easy - part two”
     

    [...] Ajax Fast and Easy - part two [...]

     

    Great article!
    I’ve been working on a JavaScript framework whose sole purpose is to guide people in their development.

    I’m trying emphasize convention over configuration and DRY principles. However, I’m so deep into the framework, that it is hard to explain its concepts the newbie user.

    After reading these two articles, it’s clear to me that you have this ability. We are looking for people like you to help us with this project - making JavaScript stupidly easy.

    If you are interested in helping with an open source project, this might be your opportunity. Let me know,

    Justin

    Justin Meyer wrote on February 24th, 2008 at 1:45 pm

     

    oh, we are starting to put up information on the library at:

    http://javascriptmvc.com

    Justin Meyer wrote on February 24th, 2008 at 1:46 pm

     

    [...] Ajax Fast and Easy - part two [...]

     

    Error: clearNode is not defined
    Source File: file:///C:/ajaxHowTo/index.html
    Line: 1
    It means that “clearNode()” fails in prototype.js
    I have search this function in prototype-1.6.0.2.js but don’t find it.

    Ignacio wrote on March 8th, 2008 at 4:32 am

     

    I didn’t actually go over the clearNode function in this article, however it is in the sample code that you can download to review the source. here is clearNode, add this to your script tag in the head…
    function clearNode(node) {
    $(node).innerHTML = “”;
    }
    it just clears out the text we added with the ajax call.

    webandnow wrote on March 8th, 2008 at 10:12 am

     

    I tried the sample code above and it doesn’t work in Vista Ultimate/IE7. Firefox 2.0.0.12 works fine. Any ideas?

    trumpy81 wrote on March 8th, 2008 at 12:50 pm

     

    i mention in the article that for it to work in IE, you need to run it on a webserver, with firefox you can just run it from the folder. if you want to test out the code on a webserver, just open up the sample page i provided here

    webandnow wrote on March 8th, 2008 at 1:57 pm

     

    Sorry, I forgot to mention that I was running the code under IIS 7.

    Anyhow, it turned out to be some hidden characters that were somehow copied along with the code.

    All is working now, just as it should.

    Thanks for the great tutorial BTW. It’s been fun and informative.

    trumpy81 wrote on March 8th, 2008 at 8:25 pm

     

    thanks for the clearNode code! Great little tutorial for a newbie! Many thanks for posting this!

    opally wrote on March 14th, 2008 at 1:38 pm