2017-07-19 77 views
2

我在ColdFusion中編寫了一個Web服務,它通過檢查數據庫中的輸入值來返回消息(成功/失敗)。 要運行CFC,我直接提供的參數中的URL,就像這樣: http://localhost/AimsWeb/Authenticate2.cfc?method=AuthenticateUser&returnformat=json&CustomerID=1&username=xxx&password=xxxx 但是當我運行這個頁面,它有錯誤,如下面的結尾: enter image description here執行cfc時出錯:從函數返回的值不是json類型

這是我的CFC:

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

<cffunction name="AuthenticateUser" access="remote" httpmethod="POST" returnFormat="JSON" returntype="json"> 

<!---- 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 = Hash(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 
    FROM tblUsers u 
    WHERE u.CustomerID = <cfqueryparam cfsqltype="cf_sql_integer" value="#Form.CustomerID#"> 
    </cfquery> 



<!--- This is to check whether provided parameters are valid by checking the same in the database---> 
<cfset local.StatusStruct = StructNew()> 

<cfif form.customerid EQ "" OR form.username EQ "" OR form.password EQ ""> 
    <cfset local.StatusStruct['errorCode'] = 400> 
    <cfset local.StatusStruct['errorMessage'] = "Insufficient Input."> 

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

    <!--- Returning the status in JSON form---> 

</cfif> 
<cfreturn local.StatusStruct> 
    </cffunction> 

</cfcomponent> 

任何人都可以幫助我嗎?

+0

您將local.StatusStruct轉換爲json的地方在哪裏? –

+0

URL中的用戶名和密碼...爲什麼? – Hackerman

+0

@DanBracuk:我相信如果我們指定returnFormat =「json」,這會自動將返回值轉換爲JSON字符串..(?) – Vasu

回答

2

它的工作。 returntype=json無效。我刪除了該行,它工作。

<cffunction name="AuthenticateUser" access="remote" httpmethod="GET" returnFormat="JSON"> 

謝謝大家的幫助。