Railo jQuery JSON

Just a quick note, for the people who use jQuery and Railo. With the version 3.1.0.017 and higher of Railo and jQuery 1.3.2 you can now use returnformat=json like in CF8. No need any more for you to use serializeJson at the end of cffunction to have JSON format.

Here an example from John Wish Blog with CF8

Your CFC

<cfcomponent output="false" hint="I return data as CF native JSON">
<cffunction name="GetProducts" output="false" access="remote">

<cfset var qryExample = QueryNew("id,title") />
<cfset var ndx = "" />

<cfloop from="1" to="5" index="ndx">
<cfset QueryAddRow(qryExample) />

<cfset QuerySetCell(qryExample, "id", ndx) />
<cfset QuerySetCell(qryExample, "title", RepeatString(Chr(64 + ndx), 3)) />
</cfloop>

<cfreturn qryExample />

</cffunction>
</cfcomponent>

Your HTML/CFML pages

<script type="text/javascript"><!--
function GetProducts(){
$.getJSON(
'NewCFComponent.cfc?wsdl',
{ method : 'GetProducts', returnformat : 'json', queryformat : 'column' },
/*
When the JSON data has returned, fire this
callback function and pass in the JSON data
as it's argument.
*/
ShowProducts
);
}

function ShowProducts(qProducts){
// matches CF8 implementation of JSON...
// example:
// {"ROWCOUNT":2,"COLUMNS":["ID","TITLE"],"DATA":{"id":[1,2],"title":["AAA","BBB"]}}
if (qProducts.ROWCOUNT==0) {
alert('Sorry, no matches found');
}
else {
for (var i=0; i<qProducts.ROWCOUNT; i++) {
// loop through JSON recordset...
// we can reference the fields like this...
nId = qProducts.DATA.id[i];
sTitle = qProducts.DATA.title[i];
//Alert just to show you the result!
alert(nId+' '+sTitle);
}
}

}
GetProducts();
// -->
</script>

| View count: 82

No comments yet.

(will not be published)
Leave this field empty: