Easy Ajax : Part 2

In the first post of this series, we looked at a really basic AJAX example. We simply pulled the time and date into our page via an AJAX call.  In this post, we'll take things a step further by passing a variables in and using it in the output.

For this example, we'll take an input string and output it in reverse.

Here is a working sample:



This example is also pretty simple. We'll start again by including the Prototype script in the head of our document:

<script type="text/javascript" src="prototype.js"></script>

Next, create the necessary form fields and buttons with this code:

<input type="text" id="input_string" />
<input type="button" value="Reverse It" onclick="run_ajax();" />
<div id="ajax_div"></div>

And now for our Prototype magic...

<script type="text/javascript">        
    function run_ajax() {
         var url = '/ajax/easy2.cfm';
         var pars = 's='+escape($F('input_string'));
         var target = 'ajax_div';
         var myAjax = new Ajax.Updater(target, url, {method:'get', parameters:pars});
    }
</script>

A few things to note in the above code block.. The line starting with "var pars" passes our data over to the AJAX page. We use $F() to get the value of the field with id "input_string" $F() is shorthand syntax that is built into Prototype and can save you lots of time and headaches. The old way was to write out document.getElementById() each time, so this is a lot shorter. We also escape the string in case there were any weird characters.

On the last line, in the Ajax.Updater call, we added braces to enclose any options including a reference to our parameters. In this case, we're specifying that this will be a get request, so we'll be expecting "URL." parameters in ColdFusion. A POST request would yield "FORM." parameters in our ColdFusion page.

Lastly, we need the ColdFusion page which will receive our call via AJAX and pass back the results. That page is actually really simple in this example, containing the following code:

<cfsetting showdebugoutput="no">
<cfoutput>
#reverse(url.s)#
</cfoutput>

That's it! We've now got the basics covered and you should be able to start working some magic!
TweetBacks
Comments
Teddy Payne's Gravatar Joe,
Prototype does inded create a blissful abstraction.

From your description of the code above:
escape($F('input_string'));

Am I correct in saying that the escape() function acts similarily to CF's URLEncodedFormat() to assist in escaping out special characters to help with security of query strings, specifically for cross-site scripting?

For Perl developers, the field notation should appear somewhat similar.

T
# Posted By Teddy Payne | 2/6/06 10:28 AM
Joe Danziger's Gravatar Yes, I believe that the escape function should work just like CF's URLEncodedFormat().

This is not done so much for security as it is to make sure that the URL being passed is OK (when doing a GET request). With AJAX, you typically can't go cross-domain. I say typically because you could create some kind of harness in the background to do the actual communications, but within the page you cannot go cross-domain.
# Posted By Joe Danziger | 2/6/06 12:58 PM
Teddy Payne's Gravatar I did not know about the cross domain calls. So, url that is executed in the run_ajax() cannot call a web service from another server outside the domain? I saw an example that Open Rico used to call a XML web service to find the weather report from weather.com.

I am not trying to point out a discrepancy, but rather trying to understand the logical flow of data. If the weather widget from rico doesn't go out of the domain, then there must be an equal widget to download the data first and then the ajax call would call a local xml data file. Would this be an accurate picture?

T
# Posted By Teddy Payne | 2/6/06 5:06 PM
Joe Danziger's Gravatar Let me try and clarify that:

run_ajax() cannot call the web service directly - it must call a page within your domain. But once you call that page, you can then run any CFML you'd like in there - including calls to web services.
# Posted By Joe Danziger | 2/6/06 5:25 PM
Teddy Payne's Gravatar I wasn't trying to have you reclarify a point that I may I have missed. It makes sense to load the web service results in a template locally and then return the results to the AJAX call. The AJAX call would initiate the template that would lod the remote service.

Thank you. That does clarify a curious concern.
# Posted By Teddy Payne | 2/7/06 10:29 AM
Neil Merton's Gravatar Could you perhaps provide an example of working with Prototype and a CFC?

That would be superb :)
# Posted By Neil Merton | 8/2/06 5:08 PM
Joe Danziger's Gravatar Neil - Prototype doesn't really care what you use to get your data. Whether you're calling a CFC, or doing a CFQUERY directly in your page, all Prototype cares about is what is going to be output. It is not language specific in any way. It just takes a chunk of code and injects it back into the calling page.
# Posted By Joe Danziger | 8/9/06 5:07 PM
Adam's Gravatar What Teddy may be asking (or not, so I will) is how do you invoke a method in a cfc using prototype. I assume you would pass your function parameters using the param, but where does the actual function name go? In the url like myCFC.cfc?myFunction or is it something totally different?
# Posted By Adam | 8/2/07 9:47 AM
Anonymouse's Gravatar Good. This was really helpful thank yuo
# Posted By Anonymouse | 12/9/07 1:38 AM