【问题标题】:Send accents to mvc controller via ajax通过 ajax 向 mvc 控制器发送口音
【发布时间】:2013-01-03 22:12:00
【问题描述】:

我正在编写一个 MVC 应用程序,它使用 AJAX 向/从控制器发送和返回数据。我使用巴西葡萄牙语在文本框上写信息,例如 Hidróxido de alumínio,然后通过 ajax 将它们发送到控制器。关键是文本被截断,例如 Hidróxido de alumÃnio....

这是我的部分代码:

    $.ajax({
            url: "/Prescricoes/AddToBin",
            type: "POST",
            dataType: "json",
            data: {
                Descricao: medposologia,
                Via: $("#Vias option:selected").val(),
                DVia: $("#Vias option:selected").text(),
                CodigoPrescricao: $("#CodigoPrescricao").val()
            },
            success:
                function (data) {
                    if (data === "ERRO!") {
                        alert("Ocorreu um erro ao adicionar esta linha à prescrição.");
                        return false;
                    }
                    seeBin();
                }
        });

medposologia 是被丢弃的字符串。这是控制器的一部分:

    [HttpPost]
    public JsonResult AddToBin (PrescricaoSaidaBIN bin, string descricao, int? via, string dvia ,int codigoPrescricao )
    {
        DetalhePrescricao detalheprescricao = new DetalhePrescricao();

        detalheprescricao.DVia = dvia;
        detalheprescricao.CodigoPrescricao = codigoPrescricao;
        detalheprescricao.Descricao = descricao;
        detalheprescricao.Via = via;

        string resultado = "OK";

        try
        {
            bin.AddItem(detalheprescricao);
        }
        catch {
            resultado = "ERRO!";
        }

        return Json(resultado);
    }

字符串 descricao 收到 medposologia 已经被垃圾了!

如何以正确的方式发送带有重音和其他字符的文本?

【问题讨论】:

    标签: asp.net-mvc jquery encode diacritics


    【解决方案1】:

    使用这个

    var data = {
        Via: $("#Vias option:selected").val(),
        Descricao: medposologia,
    };
    

       $.ajax({
                url: "/Prescricoes/AddToBin",
                type: "POST",
                dataType: "json",
                data: JSON.stringify(data),
                success:
                    function (data) {
                        if (data === "ERRO!") {
                            alert("Ocorreu um erro ao adicionar esta linha à prescrição.");
                            return false;
                        }
                        seeBin();
                    }
            });
    

    请用英文名

    【讨论】:

    • Tnx 为您解答。我遇到了另一个问题:不能使用 JSON.stringify...我之前尝试过,但我收到一条消息错误:“JSON 未定义”...我尝试过 json2.js,但没有成功...跨度>
    • 所以我仍然对此感到生气。如果有人知道演练,我将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多