CFLib.org – Common Function Library Project

serializeToJSONP(data, callback)

Last updated June 11, 2009

author

Steve Good

Version: 0 | Requires: CF8 | Library: UtilityLib

Description:
This method takes two arguments, the data to be serialized and the callback in which to wrap the data. It then serializes the data and wraps it with the callback in JSONP format. This allows javascript to make cross domain requests. I will extend my facade CFCs from a base CFC that contains this method so I can call it internally.

Return Values:
Returns a string.

Example:

<cfset foo = [1,2,9,20]>
<cfoutput>#serializeToJSONP(foo, "runit")#</cfoutput>

Parameters:

Name Description Required
data Data to be converted into JSON. Yes
callback The function call that will wrap the output. Yes

Full UDF Source:

<!---
 Serializes data to JSONP format for cross domain JSON requests.
 
 @param data      Data to be converted into JSON. (Required)
 @param callback      The function call that will wrap the output. (Required)
 @return Returns a string. 
 @author Steve Good (sgood@lanctr.com) 
 @version 0, June 11, 2009 
--->
<cffunction name="serializeToJSONP" displayname="Serialize to JSONp" hint="Serializes supplied data in JSONp format" output="false" returntype="any">
    <cfargument name="data" displayName="data" type="any" hint="The data to serialize" required="true" />
    <cfargument name="callback" displayName="callback" type="string" hint="the jsonp callback to use" required="true" />
    
    <cfscript>
    var local = {};
    local.json = serializeJSON(arguments.data);
    local.jsonp = arguments.callback & '(' & local.json & ')';
    </cfscript>
    
    <cfreturn local.jsonp />
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson