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

查看:123
本文介绍了传递和通过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组件方法,返回该字符串,并指派新的字符串,以同样的ColdFusion的会议结构的Ajax调用后的元素。

我想它会很容易,但我一直有一些问题。任何人都知道我需要做什么?

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上。为了从阿贾克斯发送的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>

然后就可以根据需要(为一个真正的JS对象),使用 jsonStruct 变量从jQuery的code。当你需要它回到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 做序列化,因为有些浏览器*咳嗽* IE *咳嗽*不支持 JSON.stringify()本地。

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

更新

我已经更新的例子jQuery的code向您展示如何更新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天全站免登陆