Ho un CFC remoto che restituisce una struttura. Si chiama usando cfajaxproxy. Voglio che il JSON sia tornato in ordine per prima cosa nella struttura prima nell'oggetto JSON. Tuttavia, il JSON restituito è in ordine misto.JSON restituito dalla funzione CFC remota è fuori servizio
Ecco la funzione remota.
<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>
Questo è il cfajaxproxy che ho impostato nella parte superiore della pagina del modulo.
<cfajaxproxy cfc="validation" jsclassname="validation">
Ecco la chiamata effettuata alla funzione remota nel gestore onSubmit del mio modulo.
var v = new validation();
v.setHTTPMethod("POST");
var errors = v.validateForm(o);
Ecco i dati (o variabile sopra) che vengono inviati alla funzione nella richiesta di post.
{"formVals":{"project":"","description":""}}
Ecco la risposta JSON restituita dalla funzione.
{"message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"description":"Please enter a description.","project":"Invalid project name.","areErrors":true}
Voglio che la risposta sia nello stesso ordine in cui è stata creata la struttura che apparirebbe così.
{"project":"Invalid project name.","description":"Please enter a description.","message":{"text":"Please fix the errors, and resubmit.","type":"validation"},"areErrors":true}
In questo modo quando ho iterare la risposta che posso impostare la messa a fuoco per il primo campo modulo con un errore in esso.
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;
}
}
});
In questo momento questi luoghi si concentrano nel secondo campo della forma, descrizione, invece che nel campo del progetto.
Possibile duplicato di http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv –