Question

I am creating some JSON data to be validated through an ajax call.

Here's my constructed JSON :

{"telephone1":"66",
 "telephone2":"66",
 "fax":"66",
 "mobilePhone":"66",
 "dateEffectiveChangementAdresseOuTel":"66",
 "adresse1IdentiqueAdresse2":true}

Here's the one obtained :

{ "adresse1IdentiqueAdresse2" : true,
  "dateEffectiveChangementAdresseOuTel" : "66",
  "fax" : "66",
  "mobilePhone" : "66",
  "telephone1" : "66",
  "telephone2" : "66"
}

As you can notice, my key are reordered in alphabetical order, which I do not want.

This causes errors to be returned to the page in the 2nd order, but I need them in the 1st order. I want my error summary (Html.ValidationSummary) to follow the errors on the page (1st error = 1st field in error).

Is there any way to preserve my original order?
Or someway to bypass this?

edit

        var coord = {
            telephone1: $("#Telephone1").val(),
            telephone2: $("#Telephone2").val(),
            fax: $("#Fax").val(),
            mobilePhone: $("#MobilePhone").val(),
            dateEffectiveChangementAdresseOuTel: $("#DateEffectiveChangementAdresseOuTel").val(),
            adresse1IdentiqueAdresse2: $("#Adresse1IdentiqueAdresse2").is(":checked")
        };

        $.ajax({
            type: 'POST',
            url: urlControleur + '_ActionTransmettre',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(coord),
            success: function (data, textStatus, jqXHR) {
                if (typeof (data) == "string") {
                    window.location = data
                    MsgErreur("");
                }
                else {
                    ListeMsgErreur(data);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                handleAjaxError(XMLHttpRequest, "M000017");
            }
        });

ajax call return (in error)

["The value {0} is not valid for Effective.",
"Le numéro saisi doit respecter le format 999 999-9999",
"Le numéro saisi doit respecter le format 999 999-9999",
"Le numéro saisi doit respecter le format 999 999-9999 ou, si vous devez saisir un numéro de poste, le format est 999 999-9999 x 9999999.",
"Le numéro saisi doit respecter le format 999 999-9999"]

It's impossible to reorder the return as is.

Was it helpful?

Solution

I would review the code that returns the json. this is where it's happening. if you cannot change the code which renders the JSON then remodel the data before passing it along

return {
   telephone1: theJson.telephone1,
   telephone2: theJson.telephone2,
   fax: theJson.fax,
   mobilePhone: theJson.mobilePhone,
   dateEffectiveChangementAdresseOuTel: theJson.dateEffectiveChangementAdresseOuTel,
   adresse1IdentiqueAdresse2: thsJson.adresse1IdentiqueAdresse2
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top