User Guide
Chapter 15: Interacting with Remote Servers 245
See the CFML Language Reference for more information on JavaScript objects.
Converting CFML Data to a JavaScript Object
The following example demonstrates the transfer of a CFQUERY result set from a
CFML template executing on the server to a JavaScript object that is processed by the
browser.
The application consists of five principal sections:
• Running a data query
• Including the WDDX JavaScript utility classes
• Specifying the conversion type and the input and output variables
• Calling the conversion function
• Outputting the object data in HTML
This example uses a registered ColdFusion datasource and can be run from
ColdFusion Server.
<!--- Create a simple query --->
<CFQUERY NAME = ’q’ DATASOURCE =’snippets’>
SELECT Message_Id, Thread_id,
Username, Posted from messages
</CFQUERY>
<!--- Cache the JavaScript so that subsequent requests will --->
<!--- use the cached version rather than making additional --->
<!--- requests to the server --->
<SCRIPT LANGUAGE="JavaScript"
SRC="/CFIDE/scripts/wddx.js"></SCRIPT>
<!--- Bring in WDDX JS support objects
A <SCRIPT SRC=></SCRIPT> can be used instead
wddx.js is part of the ColdFusion distribution --->
<CFINCLUDE TEMPLATE=’/CFIDE/scripts/wddx.js’>
<!--- Use WDDX to move from CFML data to JS --->
<CFWDDX ACTION=’cfml2js’ input=#q# topLevelVariable=’q’>
Comparison of JavaScript object and deserialized XML
JavaScript CFML
x = new Object();
x.a = "Property a";
x.b = "Property b";
x = structNew();
x.a = "Property a";
x.b = "Property b";