Take a look at the two call back handlers, OnMethod1() and OnMethod2(). The first one uses explicit jQuery code to update the DOM. The second one doesn't care what is returned, it just evaluates the value using the eval() method.
Ok, now let's look at the ajax.cfm page that is being called:
<!--- Param the method. ---> <cfparam name="URL.method" type="numeric" default="1" /> <!--- Check to see which method we are using. ---> <cfif (URL.method EQ 1)> <!--- In method one, all we want to do is return the simple value to the client. ---> <cfset strReturn = "Yeah, just like that!" /> <cfelse> <!--- In method two, we don't want to just return the value, we also want to return the Javscript that tells the client what to do with the return value. ---> <cfsavecontent variable="strReturn"> $( "#output" ).html( "Oh baby, that felt good" ); </cfsavecontent> </cfif> <!--- Trim the return value. ---> <cfset strReturn = Trim( strReturn ) /> <!--- Return AJAX value length. ---> <cfheader name="content-length" value="#Len( strReturn )#" /> <!--- Stream result to the client (this will reset the content buffer to make sure no additional data is returned). ---> <cfcontent type="text/plain" variable="#ToBinary( ToBase64( strReturn ) )#" />
Both of these methods work just as expected. I really like the second method. I am not sure if like it because it is new and unfamiliar, or if I like it because the logic is on the server which is where I am more used to playing. I have no recommendations at this point - I am just enjoying this new idea. I am letting it make the evaluation rounds in my mind. I am gonna see if I can get Rob Gonda to drop in here and make sure I really had any clue what he was talking about (I don't want to be misinforming anyone).
Want to use code from this post? Check out the license.