如何在coldfusion7和jquery ajax中使用jsonutil?

我试图了解如何使用JSONutil在jquery和coldfusion之间序列化/反序列化JSON。 我坚持使用coldfusion 7所以我不能在cfc中使用returnformat='json'属性。

client.cfc:

     SELECT client_id, client_name FROM Clients WHERE client_name LIKE '%' +  + '%'     

jquery ajax调用:

 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来实现这一点?

jsonutil的网站: http ://jsonutil.riaforge.org/

(简短的说明,我的建议是首先让cfc单独工作。以这种方式调试CF问题要容易得多。在确认cfc返回所需的JSON字符串之前,不要将jquery添加到混合中。但是回到你的问题…)

该实用程序易于使用。 在您的函数内部,创建它的实例。 然后将您的查询对象传递给serializeJSON() 。 最后返回结果字符串。

注意,您的函数签名必须支持远程访问并返回一个字符串(不是查询)

       .... run cfquery .....    

您可以直接在浏览器中测试cfc(或使用cfinvoke ):

  http://localhost/path/to/client.cfc?method=getClientsByName&name=foo 

但是,查询的本机表示有点尴尬IMO。 正如兰斯所提到的,你可能更愿意返回一组结构,这是一个更标准的结构。

    ... run query ...        

看看https://stackoverflow.com/a/6257891/886591你可以使用$ getJSON

 $.ajax({ dataType: "json", url: url, data: data, success: success }); 

Ben Nadel还有一篇关于将查询转换为数组的非常有用的文章 。 由于使用查询可能会让json感到痛苦,因此首先将它们转换为数组会更容易