JSON is just a string, so you need to βprocessβ the method call before it reaches your actual level of service.
Daniel is right in that you need to create a web service-level wrapper around your service.
So, your service method is as follows:
<cffunction name="CreateSubscription" access="public" returntype="struct" output="false"> <cfargument name="listID" required="true" type="numeric"> <cfargument name="emailaddress" required="true" type="string"> <cfargument name="firstname" required="true" type="string"> <cfset var resultset = {success=false}> <cfset resultset.success = true> <cfset resultset.message = 'kerboom!'> <cfreturn resultset /> </cffunction>
Your subscription API is as follows:
<cffunction name="subscribeAPI" access="remote" returntype="struct" returnformat="json" output="false"> <cfargument name="JSONPacket" type="string" required="true" hint="data structure received from call"> <cfset var incomingData = deserializeJSON(arguments.JSONPacket)> <cfset var resultset = {success=false,message='invalid data'}> <cfif StructKeyExists(incomingData, "apiAction")> <cfif incomingData.apiAction EQ "create"> <cfset resultset = subscriptionService.createSubscription(incomingData)> </cfif> <cfelse> <cfset resultset.message = 'No API Action specified'> </cfif> <cfreturn resultset> </cffunction>
Thus, you click JSON on the subscription API, which converts the data into a structure and ensures that you have all the data you need and is passed to the subscription service. The createSubscription method in the subscription service checks to see if listid exists and checks to see if the person is signed. If the list is good and the subscription does not exist, insert the new subscription into the database, otherwise return the results that indicate what went wrong in the structure to your API level, which converts it to JSON and returns it.
The advantage of this is that you can reuse services in your application without going through the API level, and your api level processes requests for the correct service methods and makes sure that the appropriate data is available to them,
Do not skip the local area around! There may be a load on things, including all other methods in the service. Just return what is required and nothing else.
There are other ways to solve this problem that may be more accurate - for example, you can actually argue the method of calling a method on CFC from JSON. You can use cfajaxproxy to create a layer between your service and your javascript, allowing you to directly call your cfc methods as javascript functions. And I'm sure there are other solutions.
Remember ... ColdFusion == Serverside, Javascript == clientside. Separate them. Place a layer between them to handle messages.
Hope this helps.