(简要说明,我的建议是首先让cfc单独工作。用这种方法调试CF问题要容易得多。在确认cfc返回所需的JSON字符串之前,请勿将jquery添加到混合中。但是回到您的问题…)
该实用程序易于使用。在函数内部,创建它的实例。然后将您的查询对象传递到中
serializeJSON()。最后返回结果字符串。
注意,您的函数签名必须支持远程访问并返回字符串(不是查询)
<cffunction name="GetClientsByName" access="remote" returntype="string"> <cfargument name="name" type="string" required="yes"> <!--- always localize function variables ---> <cfset var util = createObject("component", "path.to.JSONUtil")> <cfset var getClientsByName = ""> .... run cfquery ..... <!--- return JSON string ---><cfreturn util.serializeJSON(getClientsByName)> </cffunction>您可以直接在浏览器中(或使用
cfinvoke)测试cfc :
http://localhost/path/to/client.cfc?method=getClientsByName&name=foo
但是,IMO的查询本机表示有点尴尬。如Lance所述,您可能更喜欢返回结构数组,这是更标准的。
<cfset var results = arrayNew(1)> <cfset var elem = ""> ... run query ... <cfloop query="getClientsByName"> <cfset elem = structNew()> <cfset elem["client_id"] = getClientsByName.client_id> <cfset elem["client_name"] = getClientsByName.client_name> <cfset arrayAppend(results, elem)> </cfloop> <cfreturn util.serializeJSON(results)>



