【问题标题】:Disabling JSON reordering禁用 JSON 重新排序
【发布时间】:2012-03-01 16:54:00
【问题描述】:

我正在创建一些 JSON 数据以通过 ajax 调用进行验证。

这是我构建的 JSON:

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

这是获得的:

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

如您所见,我的密钥按字母顺序重新排序,这是我不想要的。

这会导致错误以第二个顺序返回到页面,但我需要它们在第一个顺序。我希望我的错误摘要 (Html.ValidationSummary) 跟随页面上的错误(第一个错误 = 第一个错误字段)。

有什么方法可以保留我原来的订单吗?
或者以某种方式绕过这个?

编辑

        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 调用返回(错误)

["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"]

不可能按原样重新排序退货。

【问题讨论】:

    标签: asp.net ajax json validationsummary


    【解决方案1】:

    我会查看返回 json 的代码。这就是它发生的地方。如果您无法更改呈现 JSON 的代码,则在传递数据之前对其进行重构

    return {
       telephone1: theJson.telephone1,
       telephone2: theJson.telephone2,
       fax: theJson.fax,
       mobilePhone: theJson.mobilePhone,
       dateEffectiveChangementAdresseOuTel: theJson.dateEffectiveChangementAdresseOuTel,
       adresse1IdentiqueAdresse2: thsJson.adresse1IdentiqueAdresse2
    };
    

    【讨论】:

    • 感谢您的建议,如您所见,我在问题中添加了信息,除非我更改了服务器端操作,这是不想要的,否则它不会起作用。
    • 我会接受这个作为答案,因为它是一个可能的答案。另一个是在接收到 json 数据时在服务器端执行相同的逻辑。那个或其他过于复杂的解决方案。似乎没有简单的出路!
    • 我会质疑为什么属性必须按特定顺序排列,如果是这样,请转到源并在那里订购它们。这样您就不需要重新映射值。
    • 订购它们仅用于显示目的(显示验证摘要时)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2018-02-06
    • 2019-06-19
    相关资源
    最近更新 更多