如何在coldfusion cfc中接收JSON POST请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在coldfusion cfc中接收JSON POST请求相关的知识,希望对你有一定的参考价值。

我有一个Web服务(cfc),它将从外部应用程序捕获/接收将发布信息的JSON数据。输入请求将采用类似于以下格式的JSON:enter image description here

所以我将收到CustomerId,UserName和Password,我只需要在我的数据库中验证这些字段并返回成功/失败消息。

我的问题是 - 如何在我的cfc中接收json数据?我写了表格范围,因为我相信它会解决我的目的,但我不确定。另外如何解析json值?

以下是我的CFC:

<cfcomponent rest="true" restpath="/AimsWeb"> <!--- REST Service--->

<cffunction name="AuthenticateUser" access="remote" httpmethod="GET"  returntype="any">

<!---- Defining Arguments--->
    <cfargument name="Username" type="string" required="Yes">
    <cfargument name="Password" type="string" required="Yes">
    <cfargument name="CustomerID" type="string" required="Yes">

<!---- Setting the Form Values (which we will get from AW+) and setting it to arguments passed--->
    <cfset Form.CustomerID = arguments.CustomerID>
    <cfset Form.Username = arguments.Username>
    <cfset Form.Password = arguments.Password>

<cfif StructKeyExists (form, 'CustomerID') and StructKeyExists(form, 'UserName') and StructKeyExists (form, 'password')>
   <cfquery name="AllUsers" datasource="#Application.GomDatasource#">
    SELECT u.UserTypeID, u.UserID, u.CustomerID, u.UserName, u.Password, u.active, u.locked
     FROM tblUsers u
     WHERE u.username = 'vasu'
    AND  u.CustomerID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.CustomerId#">
    <!---  OR u.username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.userName#"> --->
<!---    OR u.password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.password#"> --->
   </cfquery>

<!--- This is to check whether provided parameters are valid by checking the same in the database--->
<cfset local.StatusStruct = StructNew()>
<!--- <cfdump var="#AllUsers#"> --->
    <cfif AllUsers.RecordCount AND (AllUsers.Active EQ 0 OR AllUsers.locked EQ 1)>
        <cfset local.StatusStruct['errorCode'] = 401>
        <cfset local.StatusStruct['errorMessage'] = " User Account is locked">
    <cfelse>

        <cfif form.customerid EQ "" OR form.username EQ "" OR form.password EQ "">
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Insufficient Input. Please provide Customer ID, UserName and Password">

        <cfelseif AllUsers.RecordCount AND form.CustomerId EQ AllUsers.CustomerID AND form.username EQ AllUsers.UserName AND form.password EQ AllUsers.Password>
            <cfset local.StatusStruct['errorCode'] = 200>
            <cfset local.StatusStruct['errorMessage'] = " Success">

        <cfelseif AllUsers.CustomerID NEQ form.CustomerID>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Customer Id doesn't exist">

         <cfelseif AllUsers.UserName NEQ form.UserName>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " User not found">

         <cfelseif AllUsers.Password NEQ form.password>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Invalid Password">

        </cfif>

    </cfif>
</cfif>
<cfreturn local.StatusStruct> <!--- Returning the status in JSON form--->
  </cffunction>



</cfcomponent>
答案

checkout post / get请求是在coldFusion中完成的

  <cfoutput> cgi.request_method=#cgi.request_method#<br/> </cfoutput>
 <cfset req = getHTTPRequestData()> 
<cfoutput> getHTTPRequestData().method=#req.method# </cfoutput>

以上是关于如何在coldfusion cfc中接收JSON POST请求的主要内容,如果未能解决你的问题,请参考以下文章

直接访问 application.cfc 会产生未捕获的 ColdFusion 错误

在 Coldfusion 中,如何初始化位于当前路径文件夹上方的组件?

在ColdFusion中存储自动交易电子邮件模板的位置

Ajax调用ColdFusion组件而不返回数据

如何在应用程序超时时重新启动Coldfusion Application Server?

ColdFusion 编写WebService 示例