如何将 JSON 字符串传递到我的 ColdFusion Web 服务和 return success/failure 消息中?

How to pass JSON string into my ColdFusion web service and return success/failure message?

我正在 ColdFusion 中编写一个简单的 RESTful 网络服务 (cfc),并希望将 JSON 字符串传递给同一服务。像这样:

我写了如下内容:

{
"CustomerID": 100,
"UserName": "test",
"Password": "xxxxx",
}

另外,我想在验证后 return 一条 success/failure 消息。 知道我应该如何实现吗?

<cfcomponent rest="true" restpath="/folder">    

<cfscript>
  record=deserializeJSON(
'{
"CustomerID": 100,
"UserName": "aimsweb",
"Password": "xxxxx",
}'
);

</cfscript>


<cffunction name="UserDetails" returnType="JSON" access="remote" HttpMethod="GET">

 <cfargument name="Username" type="string" required="Yes">
 <cfargument name="Password" type="string" required="Yes">
 <cfargument name="CustomerID" type="string" required="Yes">

 <cfset Form.CustomerID = arguments.CustomerID>
 <cfset Form.Username = arguments.Username>
 <cfset Form.Password = Hash(arguments.Password)>


<cfquery name="AW1Users" datasource="#request.app.dsn#">
        SELECT * FROM tableName where UserName='xxxx'
</cfquery>  


    <cfif AW1Users.RecordCount>
            <cfthrow errorcode="200"                 
                    detail="Success"
                    message="Success"
                    type="Application">

    <cfelseif AW1Users.CustomerID NEQ form.CustomerID>
            <cfthrow errorcode="400"                 
                    detail="Customer Id doesn't exist"
                    message="Customer Id doesn't exist"
                    type="Application">                 
    </cfif>

    <cfreturn AW1Users>
</cffunction>
</cfcomponent>

您可以在 restful 网络服务中 return cfheader。例如

未授权:

<cfheader statusCode = "401" statusText = "unauthorized">

<cfheader statusCode = "401" statusText = "Customer Id doesn't exist">

授权:

<cfheader statusCode = "200" statusText = "Authorized">

<cfheader statusCode = "200" statusText = "Success">

对 coldfusion url 有帮助 restful 网络服务:

http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html

JSON:

    <cfquery datasource="xxx" name="qGetRecords">
        select userId, login, email from users limit 0,3
    </cfquery>

    <cfset dataset = [] />

    <cfloop query="qGetRecords">
        <cfset record = {} />
        <cfset record["one"] = qGetRecords.userId />
        <cfset record["two"] = qGetRecords.login />
        <cfset record["three"] = qGetRecords.email />
        <cfset ArrayAppend(dataset, record) />
    </cfloop>

<!--- You can return this JSON  -->  
    <cfoutput>
        <p>#SerializeJSON(qGetRecords)#</p>
        <p>#SerializeJSON(dataset)#</p>
    </cfoutput>

希望对您有所帮助