从远程 CFC 函数返回的 JSON 乱序
我有一个返回结构的远程 CFC。使用 cfajaxproxy 进行调用。我希望返回的 JSON 按顺序排列,即首先进入结构,然后进入 JSON 对象。但是,返回的 JSON 的顺序是混乱的。
这是远程功能。
<cfcomponent displayname="validation" hint="">
<cffunction name="validateForm" displayname="validateForm" hint="" access="remote" verifyClient="yes" returntype="struct">
<cfargument name="formVals" type="struct" required="yes">
<cfset errors = StructNew()>
<cfif formVals.project neq "project">
<cfset errors["project"] = "Invalid project name." />
</cfif>
<cfif Len(formVals.description) eq 0>
<cfset errors["description"] = "Please enter a description." />
</cfif>
<cfif StructIsEmpty(errors)>
<cfset errors["message"]["type"] = "success">
<cfset errors["message"]["text"] = "Client and server-side validation passed successfully.">
<cfset errors["areErrors"] = false>
<cfelse>
<cfset errors["message"]["type"] = "validation">
<cfset errors["message"]["text"] = "Please fix the errors, and resubmit.">
<cfset errors["areErrors"] = true>
</cfif>
<cfreturn errors />
</cffunction>
</cfcomponent>
这是我在表单页面顶部设置的 cfajaxproxy。
<cfajaxproxy cfc="validation" jsclassname="validation">
这是在我的表单的 onSubmit 处理程序中对远程函数进行的调用。
var v = new validation();
v.setHTTPMethod("POST");
var errors = v.validateForm(o);
这是发送到 post 请求中的函数的数据(上面的 o 变量)。
{"formVals":{"project":"","description":""}}
这是从函数返回的 JSON 响应。
{"message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"description":"Please enter a description.","project":"Invalid project name.","areErrors":true}
我希望响应的顺序与创建结构的顺序相同,如下所示。
{"project":"Invalid project name.","description":"Please enter a description.","message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"areErrors":true}
这样,当我迭代响应时,我可以将焦点设置到第一个有错误的表单字段。
var focusSet = false;
$.each(errors, function(key, val){
//alert(key + ': ' + val);
if(key != 'message' && key != 'areErrors') {
var fi = $('#' + key).parents('.formItem').filter(':first');
fi.addClass("inError");
fi.find('.err').filter(':first').html(val);
if(!focusSet) {
$('#' + key).focus();
focusSet = true;
}
}
});
现在,这将焦点放在表单的第二个字段“描述”中,而不是项目字段中。
I have a remote CFC that returns a structure. It is called using cfajaxproxy. I want the JSON returned to be in order i.e. first into the structure first into the JSON object. However, the JSON that is returned is in a mixed up order.
Here's the remote function.
<cfcomponent displayname="validation" hint="">
<cffunction name="validateForm" displayname="validateForm" hint="" access="remote" verifyClient="yes" returntype="struct">
<cfargument name="formVals" type="struct" required="yes">
<cfset errors = StructNew()>
<cfif formVals.project neq "project">
<cfset errors["project"] = "Invalid project name." />
</cfif>
<cfif Len(formVals.description) eq 0>
<cfset errors["description"] = "Please enter a description." />
</cfif>
<cfif StructIsEmpty(errors)>
<cfset errors["message"]["type"] = "success">
<cfset errors["message"]["text"] = "Client and server-side validation passed successfully.">
<cfset errors["areErrors"] = false>
<cfelse>
<cfset errors["message"]["type"] = "validation">
<cfset errors["message"]["text"] = "Please fix the errors, and resubmit.">
<cfset errors["areErrors"] = true>
</cfif>
<cfreturn errors />
</cffunction>
</cfcomponent>
This is the cfajaxproxy that I have set at the top of my form page.
<cfajaxproxy cfc="validation" jsclassname="validation">
Here's the call made to the remote function in the onSubmit handler of my form.
var v = new validation();
v.setHTTPMethod("POST");
var errors = v.validateForm(o);
Here's the data (o variable above) that is sent to the function in the post request.
{"formVals":{"project":"","description":""}}
Here's the JSON response returned from the function.
{"message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"description":"Please enter a description.","project":"Invalid project name.","areErrors":true}
I want the response to be in the same order as the structure was created which would look like this.
{"project":"Invalid project name.","description":"Please enter a description.","message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"areErrors":true}
That way when I iterate over the response I can set the focus to the first form field with an error in it.
var focusSet = false;
$.each(errors, function(key, val){
//alert(key + ': ' + val);
if(key != 'message' && key != 'areErrors') {
var fi = $('#' + key).parents('.formItem').filter(':first');
fi.addClass("inError");
fi.find('.err').filter(':first').html(val);
if(!focusSet) {
$('#' + key).focus();
focusSet = true;
}
}
});
Right now this places focus in the second field of the form, description, instead of in the project field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ColdFusion 结构体的键永远不会以任何特定顺序存储。但是,我发现一篇文章显示如何创建 java LinkedHashMap(CF Struct 下的 java)来按特定顺序存储和检索键。
编辑:丹的解决方案(数组而不是结构)可能会容易得多。
The keys of a ColdFusion struct are never stored in any particular order. However, I found one post that shows how you can create a java LinkedHashMap (which is the java underneath a CF Struct) to store and retrieve keys in a specific order.
EDIT: Dan's solution (array instead of struct) would probably be much easier.
除非您手动构建字符串来返回该数据,否则您无法(轻松)控制返回的 JSON 结构数据的顺序。如果您必须依赖订单,那么您需要在数组而不是结构中返回错误。您甚至可以返回错误结构数组,CF 将维护正确的数组顺序。
我会像这样返回您的数据:
这会给您一个单独的错误数组来循环,而不是同时处理您的基本
message
和areErrors
键你的错误。将它们完全分解为一个单独的实体,这样您就可以更轻松地在客户端循环遍历它们。You can't (easily) control the order of the returned JSON structure data unless you manually build the string to return that data. If you must be reliant on an order, then you need to return your errors in an array instead of a structure. You can even return an array of error structures, and CF will maintain the correct array order.
I would return your data like so:
This would give you a separate array of errors to loop over, instead of dealing with your base
message
andareErrors
keys at the same time as your errors. Break them out into a separate entity altogether, and you'll have an easier time of looping through them on the client side.struct 在 CFML 中没有排序(它只是一个类似 Hashmap 的集合)。
如果您想要有序结构,请使用
struct is not ordered in CFML (it's just a Hashmap-like collection).
If you want ordered struct, use
如果你想让某些东西保持秩序,最简单的方法是使用数组而不是结构。
The easiest way if you want something to stay in order use an Array instead of a structure.