如何使用jsonutil与coldfusion7和jQuery AJAX? [英] How to use jsonutil with coldfusion7 and jquery ajax?

查看:177
本文介绍了如何使用jsonutil与coldfusion7和jQuery AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用JSONutil序列化/反序列化JSON的jQuery和ColdFusion之间。我坚持的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

然而,查询的本土再presentation是一个有点尴尬海事组织。作为兰斯提到,您可以preFER返回结构而不是一个数组,这是一种比较标准的。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆