CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 03/21/2007 in coldfusion


Every CFAJAX example I've seen thus far has only shown the transmission of simple datatypes from Javascript to a CFAJAX handler Coldfusion file. I wanted to go a step further and bring a completely native Javascript JSON object into Coldfusion.

Make sure you have the newest implementations of the following:
The goal is to send the Javascript object in the serialized JSON string to the Coldfusion CFAJAX handler - after a little massaging. I immediately found that the single quote ' and pound/number # symbols - if embedded in strings in the object - will throw Coldfusion errors.

To simply attack this problem, simply use the Javascript escape() function or encodeURI() on the JSON serialized string prior to sending through the _execute function. This essentially "URL encodes" the JSON-serialized string making it safe for Coldfusion to receive. For example:
DWREngine._execute(AJAXLocation, null, 'remoteFunction', escape(JSVariable.toJSONString()), handleResponse);


On the receiving end - we must simply URLDecode the incoming string with a little extra massaging and then it is safe to use the Json.cfc's decode() function. We must parse out the # symbol which is ever-sensitive in the world of coldfusion. I found it better to completely strip this character from the incoming string prior to JSON-deserialization. For example:
<cfset procReq = URLDecode(arguments.incomingReq)>
<cfset procReq = REReplace(procReq , "##", "", "all")>
<cfinvoke component="Json" method="decode" data="#procReq #" returnvariable="processedReq">
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar