如何将 STRUCT - OR - JSON 传递给 Coldfusion CFC 方法

How to pass STRUCT - OR - JSON to Coldfusion CFC Method(如何将 STRUCT - OR - JSON 传递给 Coldfusion CFC 方法)

本文介绍了如何将 STRUCT - OR - 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,该 API 将数据转换为结构,并确保您拥有所有可用的正确数据并将其传递给您的订阅服务.订阅服务中的 createSubscription 方法检查 listid 是否存在并检查人员是否已订阅.如果列表正确且订阅不存在,则将新订阅插入数据库,否则将指示结构中出现问题的结果返回到 API 层,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 == 服务器端,Javascript == 客户端.将它们分开.在它们之间放置一个层来处理通信.

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

希望对您有所帮助.

这篇关于如何将 STRUCT - OR - JSON 传递给 Coldfusion CFC 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 STRUCT - OR - JSON 传递给 Coldfusion CFC 方法

基础教程推荐