Use jQuery, then you can use the getJSON()
method to send data back to CF web services (i.e. functions with access="remote"
), and return results back to JS again.
With CF8's returnformat="json"
on functions, this is nice and easy.
(With earlier versions, you need to use cfjson (or similar) to convert data.)
As a quick (and entirely untested) example...
<cffunction name="MyRemoteFunc" returntype="Struct" access="remote" returnformat="json">
<cfargument name="Name" type="String"/>
<cfargument name="Birthday" type="Date"/>
<cfset Session.UserDetails.Name = Arguments.Name />
<cfset Session.UserDetails.Age = DateDiff('yyyy',Now(),Arguments.Birthday) />
<cfset Session.UserDetails.Id = RandRange(1,100) />
<cfreturn Session.UserDetails />
</cffunction>
<script type="text/javascript">
function setUserDetails(name,birthday)
{
$j.getJSON
( 'http://mydomain/stuff/remote.cfc?method=MyRemoteFunc'
, { name:arguments.name , birthday:arguments.birthday }
, setUserDetailsResponse
);
}
function setUserDetailsResponse( result )
{
alert( 'Hello '+result.name+', you are '+result.age' years old and your Id is '+result.id+'.' );
}
</script>