DOJO/JSON requests in ColdFusion
Recently I was putting together a sample app for my presentation at AjaxWorld, and I decided to use the DOJO ajax library and ColdFusion together. However, there was one problem, DOJO does everything with JSON requests/response, instead of url/form variables and XML and it does it in such a way that you can't easily get to it from CF.
What I mean by that, DOJO will send the JSON request arguments in a string, in the body of the HTTP get request. Now this is perfectly valid in HTTP, however it is rare. And it means that the values don't end up in a FORM or URL variable either. So getting the JSON string can be tricky.
So I've put together a simple UDF that you can use to get this JSON request, technically anything in the http body of a request. Soon I'll put this on cflib, but for now, here it is. Hope it helps.
(Special thanks to Christian Cantrell, the byte array code is from his blog).
<cfscript>
var size=GetPageContext().getRequest().getInputStream().available();
var emptyByteArray = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray();
var byteClass = createObject("java", "java.lang.Byte").TYPE;
var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, size);
GetPageContext().getRequest().getInputStream().readLine(byteArray, 0, size);
createObject('java', 'java.lang.System').out.println("{GetJSONRequest} ByteArray.ToString=" &ToString( byteArray ) );
return ToString( byteArray );
</cfscript>
</cffunction>
<cfset jsonstring = getJSONRequest()>

There are no comments for this entry.
[Add Comment]