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

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

问题描述

我有一个ColdFusion会话变量是一个数据结构。我的目标是执行一个通过Ajax执行两件事情的jQuery调用:


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


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

我认为这很简单,但是我遇到了一些问题。任何人都知道我需要做什么?

解决方案

嗯,CF会话结构和jQuery在两个不同的领域中运行 - CF浏览器中的服务器和jQuery。为了从Ajax将ColdFusion结构发送给[cfc],您必须将会话结构序列化为一个json字符串,然后以该方式将该json字符串传输给客户端。很可能,您需要这样做才能将页面呈现给客户端:

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

然后,您可以使用 jsonStruct 变量你需要的jQuery代码(作为一个真正的JS对象)。当您需要将其发送回CF时,您可以在Javascript端重新序列化,如下所示:

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

请注意,您应该包括 json2.js 进行序列化,因为某些浏览器咳嗽 IE 咳嗽不支持 JSON.stringify()



更新



我已经更新了示例jquery代码,以显示如何更新javascript对象以使用CFC的响应。要正常工作,您的CF需要看起来像这样:

 < cffunction name =myMethodaccess =remote returnFormat =json> 
< cfargument name =myStructtype =string>

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

< cfset session.myStruct = realStruct><!---或者你现在想要做的任何事情--->

< cfreturn session.myStruct>
< / cffunction>


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. sends that ColdFusion structure to a ColdFusion component method, updates an element of that structure with a newly created string, and returns that same structure back.

or

  1. executes a ColdFusion component method that creates a new string, returns that string, and assigns that new string to an element of that same ColdFusion session structure after the Ajax call.

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

解决方案

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>

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;
   }
});

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

Update

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天全站免登陆