How would one go about implementing JSONP?

I am testing Coldbox 3.5 beta and I want to know if there is an easy way to extend the renderData or JSON plugin to support JSONP?

JSONP is basically outputting json data wrapped in a callback function.

Jeremy R. DeYoung


I just do a view with #rc.callback#(#SerializeJSON(rc.jsonObject)#)

That does work but I am trying to keep with the other standards i’m using, event.renderData rather than creating a new view/function/layout.

I’ve nearly got it to work using renderData as I have extended the requestContextDecorator but I am having trouble extending the Utilities.cfc to replace the marshallData.

Currently, I added a few lines of code in Utilities.cfc that does the callback output

args.queryKeyCase = arguments.jsonCase; args.keyCase = arguments.jsonCase; args.data = arguments.data; args.queryFormat = arguments.jsonQueryFormat; // marshal to JSON results = arguments.callback & "{" & getPlugin("JSON").encode(argumentCollection=args) & "}";

but there appears to be a better way than to change the Coldbox Framework.

Any ideas?

Jeremy R. DeYoung


This was actually a ticket at one point so if you can can contribute it we will add it


The attached requestCollection.cfc is my custom requestContextDecorator. I added

case “JSONP” :{
rd.contentType = “application/javascript”;
rd.type = “JSONP”;
rd.callback = url[‘callback’];
}else if(structKeyExists(form,‘callback’)){
rd.callback = form[‘callback’];
rd.callback = “callback”;

and also added JSONP to the list of valid rendering types. I wasn’t sure what was the best method to handle the ‘callback’ so I just added a IF statement to handle it from within the file.

Also, I changed Line 188 and added Lines 207-216 in Utilities.cfc Plugin under /system/plugins/

Jeremy R. DeYoung


requestCollection.cfc (2.46 KB)

Utilities.cfc (11.1 KB)

I just did basically the same thing. I made a pull request and fired it off to Luis.

You can see my changes here: https://github.com/ColdBox/coldbox-platform/pull/54

done, in 3.5 now with some added pizzas http://wiki.coldbox.org/wiki/WhatsNew:3.5.0.cfm

Luis F. Majano
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano


you’re awesome!

Jeremy R. DeYoung