
| home | AJAX (8) || C#.NET (7) || Coldfusion Development (16) || DHTML (15) || Flash Development (19) || jQuery (8) || MSSQL (2) || UNIX (10) |
| 3.21.07 | Send Native JSON Javascript Objects Back to CFAJAX |
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">
No comments