如何从类型为 "application/json" 的 CFFUNCTION 创建输出?
How can I create output from a CFFUNCTION with type "application/json"?
我有一个 cfc,它创建一个包含 JSON 的输出字符串。有效负载很好,但接收它的人告诉我数据以 text/html 形式传入,他的程序无法将其识别为有效,因为它需要 application/json.
所以我修改了 CFFUNCTION 语句中的参数,并构建了一个小测试程序,上面写着:
<cffunction name="Test" httpmethod="get" returntype="JSON" output="yes" access="remote">
<cfset JSON='{"Test": "A"}'>
<cfreturn JSON>
</cffunction>
但是当我远程执行此 cfc 时,我收到错误消息“从 Test 函数编辑的值 return 不是 JSON 类型”。
这应该很简单,但我看不出我需要对 return 类型为 application/json 的 JSON 字符串做什么。
您的第一个问题是 returntype="JSON"
- 您似乎混淆了 returntype
和 returnformat
。有多种方法可以实现您想要做的事情,例如,每种方法都会 return 相同的结果:
<!--- Function returns a struct, ask CF to convert it to JSON using returnformat: --->
<cffunction name="Test2" returntype="struct" returnformat="JSON" output="false" access="remote">
<cfset var myStruct={"Test"="A", "foo"="bar"} />
<cfreturn myStruct />
</cffunction>
<!--- Function uses a struct, but serializes as a JSON string before returning --->
<cffunction name="Test3" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myStruct={"Test"="A", "foo"="bar"} />
<cfreturn SerializeJSON(myStruct)>
</cffunction>
<!--- Same as above but another variation of struct notation.
Note that most CF environments will convert unquoted key names to uppercase
unless you enable serialization.preserveCaseForStructKey at application or CF Admin level --->
<cffunction name="Test4" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myStruct=structNew() />
<cfset myStruct['Test'] = "A" />
<cfset myStruct['foo'] = "bar" />
<cfreturn SerializeJSON(myStruct) />
</cffunction>
<!--- Manually build the JSON string yourself from scratch, beware of encoding/escaping issues --->
<cffunction name="Test5" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myJSONString='{"Test":"A","foo":"bar"}' />
<cfreturn myJSONString />
</cffunction>
请注意,仅以上内容不会将内容类型响应 header 设置为 application/json
,如果您的来电者确实关心这一点,那么您将需要类似以下内容:
<cfheader name="Content-Type" value="application/json; charset=utf-8" />
...但是您提到的 The value returned from the Test function is not of type JSON
错误是 CF 运行时错误,而不是 browser/ajax 错误。该错误的全文通常为:
The value returned from the Test function is not of type JSON. If the component name is specified as a return type, it is possible that either a definition file for the component cannot be found or is not accessible.
我有一个 cfc,它创建一个包含 JSON 的输出字符串。有效负载很好,但接收它的人告诉我数据以 text/html 形式传入,他的程序无法将其识别为有效,因为它需要 application/json.
所以我修改了 CFFUNCTION 语句中的参数,并构建了一个小测试程序,上面写着:
<cffunction name="Test" httpmethod="get" returntype="JSON" output="yes" access="remote">
<cfset JSON='{"Test": "A"}'>
<cfreturn JSON>
</cffunction>
但是当我远程执行此 cfc 时,我收到错误消息“从 Test 函数编辑的值 return 不是 JSON 类型”。
这应该很简单,但我看不出我需要对 return 类型为 application/json 的 JSON 字符串做什么。
您的第一个问题是 returntype="JSON"
- 您似乎混淆了 returntype
和 returnformat
。有多种方法可以实现您想要做的事情,例如,每种方法都会 return 相同的结果:
<!--- Function returns a struct, ask CF to convert it to JSON using returnformat: --->
<cffunction name="Test2" returntype="struct" returnformat="JSON" output="false" access="remote">
<cfset var myStruct={"Test"="A", "foo"="bar"} />
<cfreturn myStruct />
</cffunction>
<!--- Function uses a struct, but serializes as a JSON string before returning --->
<cffunction name="Test3" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myStruct={"Test"="A", "foo"="bar"} />
<cfreturn SerializeJSON(myStruct)>
</cffunction>
<!--- Same as above but another variation of struct notation.
Note that most CF environments will convert unquoted key names to uppercase
unless you enable serialization.preserveCaseForStructKey at application or CF Admin level --->
<cffunction name="Test4" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myStruct=structNew() />
<cfset myStruct['Test'] = "A" />
<cfset myStruct['foo'] = "bar" />
<cfreturn SerializeJSON(myStruct) />
</cffunction>
<!--- Manually build the JSON string yourself from scratch, beware of encoding/escaping issues --->
<cffunction name="Test5" returntype="string" returnformat="plain" output="false" access="remote">
<cfset var myJSONString='{"Test":"A","foo":"bar"}' />
<cfreturn myJSONString />
</cffunction>
请注意,仅以上内容不会将内容类型响应 header 设置为 application/json
,如果您的来电者确实关心这一点,那么您将需要类似以下内容:
<cfheader name="Content-Type" value="application/json; charset=utf-8" />
...但是您提到的 The value returned from the Test function is not of type JSON
错误是 CF 运行时错误,而不是 browser/ajax 错误。该错误的全文通常为:
The value returned from the Test function is not of type JSON. If the component name is specified as a return type, it is possible that either a definition file for the component cannot be found or is not accessible.