2017-07-11 61 views
0

我正在ColdFusion中編寫一個簡單的RESTful Web服務(cfc),並希望將JSON字符串傳遞給相同的服務。事情是這樣的:如何將JSON字符串傳遞到我的ColdFusion Web服務並返回成功/失敗消息?

我已經寫了類似如下:

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

另外,我想驗證後返回成功/失敗消息。 任何想法我應該如何實現這一目標?

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

回答

2

您可以在restful webservice中返回cfheader。例如

未經授權:

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

OR

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

授權:

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

OR

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

ColdFusion的RESTful Web服務有用網址:

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> 

希望這將有助於

+0

謝謝!!我很感激。 – Vasu

+0

但任何想法應該如何將JSON傳遞給此Web服務? – Vasu

+0

檢查更新的答案..希望它會有所幫助 –

相關問題