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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson