Tôi có một CFC từ xa trả về cấu trúc. Nó được gọi là sử dụng cfajaxproxy. Tôi muốn JSON được trả về theo thứ tự, tức là đầu tiên vào cấu trúc đầu tiên vào đối tượng JSON. Tuy nhiên, JSON được trả về là theo thứ tự hỗn hợp.JSON được trả lại từ chức năng CFC từ xa đã hết trật tự
Đây là chức năng từ xa.
<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>
Đây là cfajaxproxy mà tôi đã đặt ở đầu trang biểu mẫu của tôi.
<cfajaxproxy cfc="validation" jsclassname="validation">
Đây là cuộc gọi được thực hiện với chức năng từ xa trong trình xử lý onSubmit của biểu mẫu của tôi.
var v = new validation();
v.setHTTPMethod("POST");
var errors = v.validateForm(o);
Đây là dữ liệu (o biến ở trên) được gửi đến hàm trong yêu cầu bài đăng.
{"formVals":{"project":"","description":""}}
Đây là phản hồi JSON được trả về từ hàm.
{"message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"description":"Please enter a description.","project":"Invalid project name.","areErrors":true}
Tôi muốn phản hồi theo thứ tự như cấu trúc được tạo trông giống như thế này.
{"project":"Invalid project name.","description":"Please enter a description.","message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"areErrors":true}
Bằng cách đó khi tôi lặp lại phản hồi, tôi có thể đặt trọng tâm vào trường biểu mẫu đầu tiên có lỗi trong đó.
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;
}
}
});
Hiện tại, địa điểm này tập trung vào trường thứ hai của biểu mẫu, mô tả, thay vì trong trường dự án.
Bản sao có thể có của http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv –