如何通过STRUCT - 或 - JSON来ColdFusion的CFC方法 [英] How to pass STRUCT - OR - JSON to Coldfusion CFC Method

查看:231
本文介绍了如何通过STRUCT - 或 - JSON来ColdFusion的CFC方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的CFC传递结构进入方法时工作正常。

I have an existing CFC that works fine when passing structures into the method.

现在的问题是,我们现在也需要将数据传递到通过JSON相同的功能。

The problem is, we also now need to pass data into the same function via JSON.

下面是CFC片段:

<cffunction 
  name="subscribeAPI" 
  access="remote" 
  returntype="struct" 
  returnformat="json" 
  output="false">

  <cfargument 
    name="structure" 
    type="struct" 
    required="true" 
    hint="data structure received from call">

<cfif StructKeyExists(arguments.structure, "listID") 
  AND len(arguments.structure.listID)>
 ...
</cfif>

<cfreturn LOCAL />

下面是我们如何传递结构:

<cfset preStruct = {
  apiAction="Create",
  listID="1463",
  email="#form.cartEmail#",
  firstname="#form.first_name#",
  preCart="#now()#",
  planDescription="#application.name.site#"
  }
/>

<cfscript>voidReturn = application.goxObj.subscribeAPI(preStruct);</cfscript>

现在,我们还需要通过以下,但显然越来越由于CFC错误期待一个结构:

Now, we also need to pass in the following but are obviously getting errors due to the CFC expecting a structure:

function HandleSubscribe(){
  $j.getJSON(
    "/com/list.cfc?wsdl",
    {
      method : "subscribeAPI",
      action : "Create",
      listID : $j( "#listID" ).val(),
      triggerKey : $j( "#triggerKey" ).val(),
      email : $j( "#emailNL" ).val(),
      firstname : $j( "#firstnameNL" ).val()
    },
  handleSubscribeCallback
);

}

如何才能顺利通过了的getJSON片段?

感谢。

推荐答案

JSON只是一个字符串,所以你需要处理方法调用它达到你的实际服务层之前。

JSON is just a string, so you need to "handle" the method call before it reaches your actual service layer.

Danimal是正确的,这就是你需要做的是建立在你的服务的Web服务层包装。

Danimal is right in that what you need to do is create a web service layer wrapper around your service.

所以,你的服务的方法是这样的:

So your service method looks like this :

<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}>

    <!--- Validate your listid and subscription details --->
    <!--- If Valid Then insert subscription --->
    <cfset resultset.success = true>

    <!--- else --->
    <cfset resultset.message = 'kerboom!'>

    <!--- only return what you need as a struct, not the whole local scope! --->
    <cfreturn resultset />
 </cffunction>

您的订阅API是这样的:

Your subscription API looks like this :

<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">
           <!--- You should also check you have the required fields for the createSubscription method here too. --->
           <cfset resultset = subscriptionService.createSubscription(incomingData)>
       </cfif>
   <cfelse>
       <cfset resultset.message = 'No API Action specified'>
   </cfif> 

   <cfreturn resultset>
</cffunction>

所以,你推JSON在认购API,它的数据到一个结构转换,并确保您拥有所有正确的可用数据,并将其传递给您的订阅服务。在订阅服务检查createSubscription方法以查看是否listid存在并检查是否该人已经订阅。如果列表是好的,订阅不存在,将新的订阅到数据库,否则返回结果,表明了什么问题的结构,以你的API层,将其转换成JSON并返回。

So you push the JSON at the subscribe API, which converts the data to a struct and makes sure you have all the right data available and passes it off to your subscription service. The createSubscription method in the subscription service checks to see if the listid exists and checks to see if the person is already subscribed. If the list is good and the subscription doesn't exist, insert the new subscription into the database, otherwise return results that indicate what went wrong in a struct to your API layer, which converts it to JSON and returns it.

这样做的好处是,你可以不必再通过API层和你的API层手柄推请求到正确的服务方法,并确保没有为他们提供适当的数据在应用程序中重复使用的服务。

The benefit to this is you can re-use the services in your application without having to go through the API layer and your api layer handles pushing the requests to the correct service methods and making sure that there is appropriate data available for them.

不要通过周围局部范围!可以有东西一棚负载在那里,包括所有在服务中的其他方法。刚刚返回需要什么,仅此而已。

Don't be passing the local scope around! There can be a shed load of stuff in there including all the other methods in the service. Just return what is required and nothing more.

有就可以解决这个可能是整洁的其他方式 - 例如,你可以居然把争论变成一个方法调用的方法从JSON一个CFC。你可以使用cfajaxproxy为您服务,你的JavaScript之间的创建图层,您可以直接打电话给你的CFC方法的JavaScript函数。我敢肯定,有对这些顶级的其他解决方案。

There are other ways you can solve this that might be neater - for example you can actually put arguments into a method method call on a CFC from JSON. You could use cfajaxproxy to create the layer between your service and your javascript, enabling you to call your cfc methods directly as javascript functions. And I'm sure there are other solutions on top of these.

记住.... ColdFusion的== Serverside集团,Javascript的==客户方。将它们分开。放一层它们之间处理通信。

Remember.... ColdFusion == Serverside, Javascript == clientside. Separate them. Put a layer between them to handle communications.

希望有所帮助。

这篇关于如何通过STRUCT - 或 - JSON来ColdFusion的CFC方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆