ColdFusion 9.0 Resources |
Using data interchange formatsAll complex data that is communicated over an HTTP connection must be serialized into a string representation that can be transmitted over the web. Most commonly, web client applications use XML or JSON. As a general rule, ColdFusion automatically handles all necessary serialization and deserialization when you use ColdFusion Ajax features. The proxies that you create with the cfajaxproxy tag, and the bind expressions that call CFC functions automatically request data in JSON format, and automatically deserialize JSON data to JavaScript variables. ColdFusion also provides the capability to create, convert, and manage data in web interchange formats. This is helpful, for example, if you use custom Ajax elements to get data from ColdFusion servers. Also, you use ColdFusion data serialization capability for any applications that create or consume complex data transmitted over an HTTP connection. For example, if you want to make a web service or feed available in JSON format, many Yahoo! web services currently are accessible by using simple URLS that return data as JSON. Note: For information on ColdFusion tags and functions
for handling XML or WDDX data, see Using XML and WDDX.
Controlling CFC remote return value data formatBy default, CFC functions convert data that they return to remote callers to WDDX format. However, they can also return the data in JSON format, or as plain string data. (XML objects are automatically converted to string representation when returning plain data.) ColdFusion Ajax elements that request data from CFC functions, including bind expressions and the function proxies generated by the cfajaxproxy tag, automatically generate a returnFormat parameter in the HTTP URL to request JSON data from the CFC function. Control the CFC function return format in the following ways:
If the requested return format is JSON and the function returns a query, ColdFusion serializes the query into a JSON object in either of the following formats:
For more information, see the SerializeJSON function in the CFML Reference. Using JSONJSON (JavaScript Object Notation) is a lightweight JavaScript-based data interchange format for transmission between computer systems. It is a much simpler format than XML or WDDX, and is an efficient, compact format for transmitting data required for Ajax applications. ColdFusion Ajax bind expressions that use CFCs tell the CFC function to send the data in JSON format by including a returnformat="json" parameter in the HTTP request, and automatically handle the JSON-formatted result. JSON represents objects by using { key : value , key : value... } notation, and represents arrays in standard [ value , value... ] notation. Values can be strings, numbers, objects, arrays, true, false, or null. Therefore, you can nest arrays and objects inside each other. For a detailed specification of the JSON format, see www.JSON.org. Although ColdFusion Ajax-based controls and the cffunction tag interoperate transparently, without you converting anything to JSON format, other applications can take advantage of JSON format data. Many public feeds are now available in JSON format. For example, the Yahoo! search interface returns a JSON data set, del.icio.us provides JSON feeds showing your posts and tags, and Blogger feeds are available in JSON format. You don’t have to use Ajax to display these feeds; use standard ColdFusion tags and functions to display the results. The following CFML functions support using JSON format in server-side code: For more information about these functions and examples, see the CFML Reference. The following example shows how to use ColdFusion JSON functions in a non-Ajax application. It does a Yahoo search for references to "ColdFusion Ajax" and displays these results:
<!--- Send an http request to the Yahoo Web Search Service. ---> <cfhttp url='http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query= "ColdFusion Ajax"&output=json'> <!--- The result is a JSON-formatted string that represents a structure. Convert it to a ColdFusion structure. ---> <cfset myJSON=DeserializeJSON(#cfhttp.FileContent#)> <!--- Display the results. ---> <cfoutput> <h1>Results of search for "ColdFusion 9"</h1> <p>There were #myJSON.ResultSet.totalResultsAvailable# Entries.<br> Here are the first #myJSON.ResultSet.totalResultsReturned#.</p> <cfloop index="i" from="1" to="#myJSON.ResultSet.totalResultsReturned#"> <h3><a href="#myJSON.ResultSet.Result[i].URL#"> #myJSON.ResultSet.Result[i].Title#</a></h3> #myJSON.ResultSet.Result[i].Summary# </cfloop> </cfoutput> |