通过 JQuery 传递和返回 ColdFusion 结构 [英] Passing and returning ColdFusion Structure via JQuery

查看:25
本文介绍了通过 JQuery 传递和返回 ColdFusion 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ColdFusion 会话变量,它是一种数据结构.我的目标是执行一个 jQuery 调用,通过 Ajax 执行以下两件事之一:

I have a ColdFusion session variable that's a structure of data. My goal is to execute a jQuery call that does one of two things via Ajax:

  1. 将该 ColdFusion 结构发送到 ColdFusion 组件方法,使用新创建的字符串更新该结构的元素,然后返回相同的结构.

  1. 执行创建新字符串的 ColdFusion 组件方法,返回该字符串,并在 Ajax 调用后将该新字符串分配给同一 ColdFusion 会话结构的元素.

我认为这很容易,但我遇到了一些问题.有人知道我需要做什么吗?

I would think it'd be easy, but I've been having some problems. Anybody know what I would need to do?

推荐答案

嗯,CF 会话结构和 jQuery 在两个不同的领域运行 - 服务器上的 CF 和浏览器中的 jQuery.为了从 Ajax 将 ColdFusion 结构发送到 [cfc]...",您必须将会话结构序列化为 json 字符串,然后以某种方式将该 json 字符串传输到客户端.很可能,您希望在向客户端呈现页面的过程中执行此操作:

Well, the CF session structure and jQuery operate in two different spheres - CF on the server and jQuery in the browser. In order to "send that ColdFusion structure to a [cfc]..." from Ajax, you'll have to have serialized the session structure as a json string and then transmitted that json string to the client somehow. Most likely, you'll want to do this as part of the rendering of the page to the client:

<cfoutput>var jsonStruct = #SerializeJSON(session.myStruct)#;</cfoutput>

然后,您可以根据需要使用 jQuery 代码中的 jsonStruct 变量(作为真正的 JS 对象).当您需要将其发送回 CF 时,您可以在 Javascript 端再次对其进行序列化,如下所示:

Then you can use the jsonStruct variable from your jQuery code as needed (as a real JS object). When you need to send it back to CF, you can serialize it again on the Javascript side, like so:

$.ajax({
   url: "foo.cfc?method=myMethod", 
   dataType: "json",
   data: {myStruct: JSON.stringify(jsonStruct)}, 
   success: function (respJSON) {
      jsonStruct = respJSON;
   }
});

请注意,您应该包含 json2.js 来进行序列化,因为某些浏览器 coughIEcough 原生不支持 JSON.stringify().

Note that you should include json2.js to do the serialization, since some browsers coughIEcough don't support JSON.stringify() natively.

更新

我更新了示例 jquery 代码,以展示如何更新 javascript 对象以使用来自 CFC 的响应.要正常工作,您的 CF 需要如下所示:

I've updated the example jquery code to show how you can update the javascript object to use the response from the CFC. To work properly, your CF will need to look something like this:

<cffunction name="myMethod" access="remote" returnFormat="json">
  <cfargument name="myStruct" type="string">

  <cfset var realStruct = DeserializeJSON(arguments.myStruct)>

  <cfset session.myStruct = realStruct><!--- or whatever you want to do with it at this point --->

  <cfreturn session.myStruct>
</cffunction>

这篇关于通过 JQuery 传递和返回 ColdFusion 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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