Ajax.oncomplete 错误地转义了单引号

Ajax.oncomplete escaping the singlequote incorrectly

我正在使用 Omnifaces ajax.oncomplete 函数在我的 JSF 2 页面中显示 toastr 消息。我面临的问题是我正在开发一个 frensh web 应用程序,我们使用了很多引号 (')。当我添加引号时,浏览器抛出格式错误的 XML 异常:

malformedXML: missing ) after argument list

虽然我使用斜杠使其忽略引号并将其视为字符串:

Ajax.oncomplete("toastr.warning('Vérifier l\'adresse e-mail saisie.')");

有没有办法传递这个异常?

\ 本身也是 Java 中的转义字符。所以,最终 \ 被 Java 剥夺了。

您需要对其进行两次转义以表示文字 \,因此它在 JavaScript 中作为真实的 \ 到达。

Ajax.oncomplete("toastr.warning('Vérifier l\'adresse e-mail saisie.')");

或者,如果这些字符串不是硬编码的,因此来自动态源,并且您基本上因此需要执行自动转义,那么最好使用 Ajax#data() 自动让 OmniFaces 编码 Java 变量作为格式正确的 JSON 对象,可通过 Java 脚本上下文中的 OmniFaces.Ajax.data 使用。

Ajax.data("message", "Vérifier l'adresse e-mail saisie.");
Ajax.oncomplete("toastr.warning(OmniFaces.Ajax.data.message)");

这样你就不用担心逃避大惊小怪了。