AJAX in ColdFusion 8: Don't overthink it!
July 17 2007 by
Adam
<cfcomponent name="AjaxTest" hint="Performs some proof of concept AJAX functionality for CFMenuCal."> <cffunction name="updateSession" access="remote" output="false" returntype="Boolean"> <cfargument name="data" type="any" required="true"> <cftry> <cfset session.data = arguments.data> <cfreturn true> <cfcatch type="any"> <!— log the error via cfmail or cffile —> <cfreturn false> </cfcatch> </cftry> </cffunction> </cfcomponent>
Notice that the I set theACCESS of the function to REMOTE. This pretty frequently gets me (I commonly use PUBLIC), and I spend too much time trying to figure out why I'm getting "method X doesn't have any properties" JavaScript errors.
So now that we have our component, we need a page that uses it. Enter test.cfm:
<cfajaxproxy cfc="com.AjaxTest"> <cfdump var="#session#" label="Session Scope"> <form method="post" name="fooForm"> <input type="text" name="bar" size="30" /> <input type="button" onClick="sessionize(this.form.bar.value);" value="Save" /> </form> <script type="text/javascript" language="javascript"> sessionize = function(data){ var updater = new com.AjaxTest(); var myObj = {}; myObj.numberValue = 42; myObj.stringValue = "hello, world!"; myObj.userValue = data; var success = updater.updateSession(myObj); if (success == "true" || success == true){ alert('Success! Refreshing the page to update dump…'); document.location.href = document.location.href; }else{ alert('An error occurred. Check the log for details…'); } } </script>
The above assumes the component is named AjaxTest and resides either in a subdirectory named "com" of the current directory, or of the root folder. Running it should produce a cfdump that looks something like this:
Posted in AJAX | CFMenuCal | ColdFusion | Scorpio |