如何使用 jQuery 创建可在 ColdFusion 函数中使用的适当的表单元素数组?
我有一个非常标准的联系表格,现在使用 cfc 进行处理。我想为启用了 javascript 的用户使用 .post 。我已经使用表单元素的 jQuery 创建了一个数组,我想将其传递给同一个 CFC(不修改 CFC),但我不确定如何执行此操作。基本上,我想将名为“formData”的内容作为参数传递给 CFC(就像我对基本服务器端代码所做的那样),然后在 CFC 中解析它。现在我只是在 cfc 中使用 cfdump(它可以很好地处理非 java 提交),但它不适用于此设置。有什么想法吗?
这是我的 jQuery
$('#theForm').submit(function(e) {
e.preventDefault();
var formData = {};
$('form [name]').each(function(){
formData[this.name] = this.value;
});
$.post("cfc/engine.cfc?method=collectdata&returnformat=json",
{'formData': formData}
);
});
和 CFC
<cffunction name="collectdata" access="remote" output="false" returntype="void">
<cfargument name="formData" type="struct" required="yes">
<cfdump var="#formData#">
<cfabort>
</cffunction>
I have a pretty standard contact form that uses a cfc for processing now. I want to use .post for users that have javascript turned on. I've created an array with jQuery of the form elements and I want to pass that to the same CFC (without modifying the CFC) but I'm not certain how to do it. Basically, I want to pass something called 'formData' as an argument to the CFC (as I do with just the basic server side code), and then parse it in the CFC. Right now I'm just using a cfdump in the cfc (which works fine with a non-java submit) but it doesn't work with this set-up. Any ideas?
Here's my jQuery
$('#theForm').submit(function(e) {
e.preventDefault();
var formData = {};
$('form [name]').each(function(){
formData[this.name] = this.value;
});
$.post("cfc/engine.cfc?method=collectdata&returnformat=json",
{'formData': formData}
);
});
And my CFC
<cffunction name="collectdata" access="remote" output="false" returntype="void">
<cfargument name="formData" type="struct" required="yes">
<cfdump var="#formData#">
<cfabort>
</cffunction>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我希望自己拥有一台 24/7 随时可用的服务器的另一次情况...
仔细观察您的 CFC,您似乎要求 formData 是一个结构体。但是,IIRC、JSON 被视为字符串,您必须手动反序列化它。
尝试将类型更改为字符串,或完全删除它。
编辑: 看起来 jquery.post() 发送的是标准 http post,而不是 AJAX/webservice 调用。因此,如果我正确理解所有分散的文档,内容将被放入表单范围中。
This is another one of those times when I wish that I had a server at my fingertimes 24/7...
Looking closer at your CFC, it lookds like you are requiring formData to be a struct. However, IIRC, JSON is considered a string, and you'll have to deserialize it manually.
Try changing the type to string, or removing it altogether.
Edit: Looks like jquery.post() sends a standard http post, not an AJAX/webservice call. So, the contents will get put into the form scope, if I'm understanding all of the scattered docs correctly.
通过将 javascript 数组序列化为 JSON 来准备它。一种简单而严格的方法是使用 Crockford 的 json2js。所以你会:
...然后:
...然后在你的
.post()
中:将你的 cfc 设置为接收
string
类型的参数(或者任何
)。在 cfc 中,对传入参数使用deserializeJson()
:因此,您获取了一个 javascript 数组,将其序列化为 JSON,将其发送到您的 cfc,cfc 以字符串形式接收它,然后将其反序列化为“原生”CF 数组。同样的事情也适用于分别在 javascript 和 CF 之间传输 js 对象/结构(与本例中的简单数组相反)。
Prepare your javascript array by serializing it to JSON. An easy and rigorous way to do this is with Crockford's json2js. So you'd have:
...and then:
...and then in your
.post()
:Set your cfc to receive an argument of type
string
(orany
). In the cfc, usedeserializeJson()
on the incoming argument:So you've taken a javascript array, serialized it to JSON, sent it to your cfc which received it as a string, then deserialized it to a "native" CF array. The same thing will work for transporting js objects/structs between javascript and CF respectively (as opposed to the simple array in this example).