How to use jsonutil with coldfusion7 and jquery ajax?(如何将 jsonutil 与 Coldfusion7 和 jquery ajax 一起使用?)
问题描述
我正在尝试了解如何使用 JSONutil 在 jquery 和 Coldfusion 之间序列化/反序列化 JSON.我被coldfusion 7卡住了,所以我不能在我的cfc中使用 returnformat='json'
属性.
I'm trying to understand how to use JSONutil to serialize/deserialize JSON between jquery and coldfusion. I am stuck with coldfusion 7 so I can't use the returnformat='json'
attribute in my cfc.
client.cfc:
client.cfc:
<cfcomponent>
<cffunction name="GetClientsByName"
returntype="query"
hint="get clients from search term">
<cfargument name="name" type="string" required="yes">
<cfquery name="GetClientsByName" datasource="#application.dsn#">
SELECT client_id, client_name
FROM Clients
WHERE client_name LIKE '%' + <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.name#"> + '%'
</cfquery>
<cfreturn GetClientsByName>
</cffunction>
</cfcomponent>
jquery ajax 调用:
jquery ajax call:
function getClients(name){
$.ajax {
type: "post"
url: "/surveymanagement/admin/client.cfc",
dataType: "json",
data: {
method: "GetClientsByName",
name: name
},
success: function(data){
$("#here").html(data)
}
}
现在我在哪里以及如何使用 jsonutil 来让它工作?
Now where and how do I use jsonutil to get this to work?
jsonutil 的站点:http://jsonutil.riaforge.org/
The site for jsonutil: http://jsonutil.riaforge.org/
推荐答案
(简要说明,我的建议是先让 cfc 单独工作.这样调试 CF 问题要容易得多.不要将 jquery 添加到混合中直到您确认 cfc 返回所需的 JSON 字符串.但回到您的问题...)
(Brief side note, my advice is get the cfc working separately first. It is much easier to debug CF problems that way. Do not add jquery to the mix until you have confirmed the cfc returns the desired JSON string. But back to your question ...)
该实用程序易于使用.在您的函数内部,创建它的一个实例.然后将您的查询对象传递给 serializeJSON()
.最后返回结果字符串.
The utility is easy to use. Inside your function, create an instance of it. Then pass your query object into serializeJSON()
. Finally return the resulting string.
注意,你的函数签名必须支持远程访问并返回一个字符串(不是查询)
<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>
您可以直接在浏览器中测试 cfc(或使用 cfinvoke
):
You can test the cfc directly in your browser (or with cfinvoke
):
http://localhost/path/to/client.cfc?method=getClientsByName&name=foo
但是,查询的本机表示在 IMO 中有点尴尬.正如 Lance 所提到的,您可能更喜欢返回一个结构数组,这是更标准的.
However, the native representation of queries is a bit awkward IMO. As Lance mentioned, you may prefer to return an array of structures instead, which is a more standard.
<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)>
这篇关于如何将 jsonutil 与 Coldfusion7 和 jquery ajax 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 jsonutil 与 Coldfusion7 和 jquery ajax 一起使用


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01