【问题标题】:Error while executing the cfc : The value returned from the function is not of type json执行 cfc 时出错:从函数返回的值不是 json 类型
【发布时间】:2025-11-23 05:25:01
【问题描述】:

我在 ColdFusion 中编写了一个 Web 服务,它通过检查数据库中的输入值来返回消息(成功/失败)。 要运行 cfc,我直接在 URL 中提供参数,如下所示: http://localhost/AimsWeb/Authenticate2.cfc?method=AuthenticateUser&returnformat=json&CustomerID=1&username=xxx&password=xxxx 但是当我运行这个页面时,它以如下错误结束:

这是我的 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>

谁能帮帮我?

【问题讨论】:

  • local.StatusStruct 转为 json 的部分在哪里?
  • 网址中的用户名和密码...为什么?
  • @DanBracuk:我相信如果我们指定 returnFormat=“json” 并且这会自动将返回值转换为 JSON 字符串.. (?)
  • @Hackerman:我只是将用户名、密码作为参数传递给运行 cfc(因为我不想编写调用页面)
  • 另外,您在浏览器中粘贴该网址,然后它是一个获取请求...您需要执行一个发布请求,因为您有这个httpmethod="POST"

标签: coldfusion cfc


【解决方案1】:

成功了。 returntype=json 无效。我删除了那条线,它起作用了。

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

感谢大家的帮助。

【讨论】:

    最近更新 更多