Converting CFML data to a JavaScript object
The following example demonstrates the transfer of a
executing on the server to a JavaScript object that is processed by the browser.
The application consists of four principal sections:
•
Running a data query
•
Including the WDDX JavaScript utility classes
•
Calling the conversion function
•
Writing the object data in HTML
The following example uses the cfsnippets data source that is installed with ColdFusion:
<!--- Create a simple query
<cfquery name = "q" datasource ="cfsnippets">
SELECT Message_Id, Thread_id, Username, Posted
FROM messages
</cfquery>
<!--- Load the wddx.js file, which includes the dump function --->
<script type="text/javascript" src="/CFIDE/scripts/wddx.js"></script>
<script>
// Use WDDX to move from CFML data to JavaScript
<cfwddx action="cfml2js" input="#q#" topLevelVariable="qj">
// Dump the recordset to show that all the data has reached
// the client successfully.
document.write(qj.dump(true));
</script>
Note: To see how
for example in wwwroot/myapps/wddxjavascript.cfm, run the page in your browser and select View
Source in your browser.
Transferring data from the browser to the server
The following example serializes form field data, posts it to the server, deserializes it, and displays
the data. For simplicity, it only collects a small amount of data. In applications that generate
complex JavaScript data collections, you can extend this basic approach very effectively. This
example uses the
deserialize the data.
To use the example:
Save the file under your webroot directory, for example in wwwroot/myapps/
1
wddxserializedeserialze.cfm.
Display http://localhost/myapps/wddxserializedeserialze.cfm in your browser.
2
Enter a first name and last name in the form fields.
3
Click Next.
4
The name appears in the Names added so far box.
Repeat steps 3 and 4 to add as many names as you wish.
5
--->
Action="cfml2js" works, save this code under your webroot directory,
cfwddx
JavaScript object to serialize the data, and the
WddxSerializer
recordset from a ColdFusion page
cfquery
tag to
cfwddx
Using WDDX
703
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers