Latest Articles:
Committee Members:
Alert Email
Get a short email alert whenever a new entry is published.
Confidential, secure it's piece of cake to keep uptodate.
OpenBD gets native JSONP support
We all know the power of web services, whether its SOAP or RESTlet based calls, the ability to consume remote services is key to any service orientated architecture.
CFML has always been blessed by its deep web services support, both in consuming remote resources (CFHTTP, CFINVOKE) and also providing rich end points through the use of CFC's.
Any CFC can be made into a remote service endpoint by simply enabling the access="remote" attribute on a function within a component.
// assume this is saved @ /rpc/mycfc.cfc
<cfcomponent>
<cffunction name="run" access="remote" returntype="string">
<cfreturn "from a remote">
</cffunction>
</cfcomponent>
This method is now available for consumption through a number of different methods:
- SOAP call: http://myhost/rpc/mycfc.cfc?WSDL
- RESTlet call: http://myhost/rpc/mycfc.cfc?method=run
The RESTlet call is most useful for Javascript based applications, allowing you to consume and trigger CFC from within any web page running on your server. A CFC called in this manner can have a number of different return formats using the returnformat="json|plain|xml" attribute of a function.
This is all standard CFML and has been available within OpenBD from the start.
Cross-Domain Support
However, what if you wish your services to be consumed by a Javascript function outside of your own domain? Cross-domain scripting will prevent this. So how do the likes of delicious.com offer such Javascript integrations for their services?
You achieve this by using JSONP (JSON with Padding) and this lets you achieve cross-domain data fetching.
JSONP requires a little assistance from both the server and the client inorder for it to work. JSONP works by wrapping the returning JSON packet in () and prepending a name to make it into a Javascript document.
The majority of Javascript libraries support JSON from their native core. JQuery supports it using $.getJSON method, which simply asks that you append the URI parameter "callback=?" to the end of the remote URL and it will do all the marshalling of data for you.
Enabling JSONP in OpenBD
Before you do that, you have to tell OpenBD that this is a JSONP request and allow it the opportunity to post-process the packet for you. You do this using the existing __BDRETURNFORMAT parameter flag and setting it to "jsonp". OpenBD will look for the "callback" parameter and the take the necessary action for you accordingly.
An example of this in action from standard JQuery:
$.getJSON(
"http://myhost/rpc/mycfc.cfc?method=run&__BDRETURNFORMAT=jsonp&callback=?",
params,
function(json){
// do something with the 'json' object
}
);
The advantage of this, is that you do not need to modify your existing CFC's inorder for them to be consumed using JSONP methods. There is also huge memory and performance gains to be had, letting the core engine do all the heavy lifting instead of the existing workarounds written in CFML.
Controlling incoming data
OpenBD has enabled a number of flags that the remote REST client can use to help format the data coming back from the server without having to touch your CFC.
- __BDRETURNFORMAT
Controls the data format of the data. Valid values: wddx, plain, json, jsonp - __BDQUERYFORMAT
Controls how query data should be formatted. Valid values: column, row - __BDNODEBUG
If present, then if the CFC creates an error, then any stack trace will be not be sent to the client
Through the use of these (case insensitive) flags you can control how you wish the data to be presented by the client instead of having to modify any server-side code, making it easier for you to offer greater flexibility to your web services clients.
JSONP support available now
This functionality has been enabled in the nightly build of OpenBD and will be available in the core release 1.3 once released.



