我有一個遠程CFC返回一個結構。它被稱爲使用cfajaxproxy。我希望返回的JSON按順序排列,即首先將結構放入JSON對象中。但是,返回的JSON按照混合順序排列。從遠程CFC功能返回的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);
下面是發送到發佈請求中的函數的數據(上面的變量)。
{"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;
}
}
});
現在這個地方的焦點集中在form,description的第二個字段中,而不是在項目字段中。
可能的重複http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv –