Coldfusion Webservices and JSON

I've recently tested CF web services and JSON. The outcome is incredible. Because, I'm very new to JSON and CF web services. That's why I'm very proud of myself what I did. That's what I want to do in Myanmar about synchronization two web applications over the internet for sharing data. In these testing, I've also used Coldfusion ajax submitForm. In briefly, when user type, then will be displayed JSON format. I've got some JSON knowledge from Ben Nadel blog. getJSON.cfc

view plain print about
1<cfcomponent output="false">
2<cffunction name="jsonData" access="remote" returntype="array" returnformat="json" output="false" hint="Return JSON Format Data">
3<cfargument name="Text" type="string" required="true">
4<cfset getJ = ArrayNew(1)>
5<cfloop from="1" to="#ListLen(Text, " ")#" index="i">
6<cfset getJ[i] = ListGetAt(Text, i, " ")>
7</cfloop>
8<cfreturn getJ>
9</cffunction>
10</cfcomponent>
get.cfm
view plain print about
1<html>
2<head>
3<cfajaximport>
4<invalidTag>
5function submitForm() {
6ColdFusion.Ajax.submitForm('myform', 1, 'formAction.cfm', 1, callback,
7errorHandler);
8}
9function callback(text)
10{
11document.getElementById("divShowMsg").innerHTML = text;
12}
13function errorHandler(code, msg)
14{
15alert("Error!!! " + code + ": " + msg);
16}
17</script>
18</head>
19<body>
20<div id="divShowMsg"></div>
21<cfform name="myform">
22<cfinput name="mytext1" size="50">
23<input type="Button" onclick="submitForm()" value="WebServices JSON ">
24</cfform>
25</body>
26</html>
formAction.cfm
view plain print about
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<title>Untitled</title>
5</head>
6<body>
7<cfinvoke webservice="http://localhost/webservices/getJSON.cfc?wsdl" method="jsonData" returnvariable="strg">
8<cfinvokeargument name="Text" value="#form.mytext1#" />
9</cfinvoke>
10<cfdump var="#strg#">
11</body>
12</html>

cfinvoke, cffunction and cfargument

I'm just intermediate level in WebServices such as cfinvoke, cffunction and cfargument The usage of these three tags are to integrate with different language of web application and share information each other. I'm not nice to clarify more of these three tags, then I'll show what I did with these three tags.

view plain print about
1<cfinvoke
2    component="ContactInformation"
3    method="GetContactInformation"
4    returnvariable="QryContactInformation">

5    
6    <cfinvokeargument name="City" value="Bagan">
7    
8</cfinvoke>
9<cfdump var="#QryContactInformation#">

ContactInformation.cfc

view plain print about
1<cfcomponent hint="Get Information of Contact">
2    <cffunction name="GetContactInformation" access="remote" returntype="query">
3        <cfargument name="City" type="string" required="yes">
4        <cfquery name="QryContactInformation" datasource="#application.mydsn#">
5            SELECT U_ID, U_Name, U_Address FROM tbl_Contacts
6        </cfquery>
7        <cfreturn QryContactInformation>
8    </cffunction>
9</cfcomponent>

Webservices

Well, I'm interesting to create webservices (WSDL) in CF in these days. Because, I wanna test how to integrate two different language by webservices. Developing webservices is very simple. Ok, let's go.

helloWorld.cfc

view plain print about
1<cfcomponent>
2<cffunction name="HelloWorldMsg" access="remote" returntype="string" output="no">
3<cfargument name="name" type="string" required="yes">
4<cfreturn "Hello World : " &amp; arguments.name>
5</cffunction>
6</cfcomponent>

caller.cfm

view plain print about
1<cfinvoke webservice="http://helloWorld.cfc?wsdl" method="HelloWorldMsg" returnvariable="msg">
2<cfinvokeargument name="name" value="PPSHEIN"/>
3</cfinvoke>
4<cfoutput>#msg#</cfoutput>

Top of Page