简体   繁体   English

使用jquery.validation和XRegexp时,如何防止传入的UTF-8内容重新编码为UTF-16?

[英]How to prevent incoming UTF-8 content from being re-encoded as UTF-16 when using jquery.validation and XRegexp?

I'm building a form that uses JQuery.Validator with XRegexp to clean input fields before they are submitted to the server (which also validates all incoming posts) but I'm having a little bit of trouble posting the content back to the server. 我正在构建一个将JQuery.Validator和XRegexp一起使用的表单,以在将输入字段提交给服务器之前清理输入字段(这还将验证所有传入的帖子),但是我有点麻烦将内容发布回服务器。

If I post a UTF-8 string like this: Русский日本語fdsffsfsa 如果我发布这样的UTF-8字符串: Русский日本语fdsffsfsa

to the form below: 到以下表格:

<form id="xyzzyForm">
    <input id="xyzzy" name="xyzzy" type="text" />
    <button type="submit">Submit</button>
</form>

my JQuery/Validate/XRegExp POST's the string as UTF-16: xyzzy=%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%E6%97%A5%E6%9C%AC%E8%AA%9Efdsffsfsa 我的JQuery / Validate / XRegExp POST的字符串为UTF-16: xyzzy =%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%E6%97%A5 %E6%9C%AC%E8%AA%9Efdsffsfsa

I know from German Rumm's response on this post Why is jQuery POSTing in UTF-16? 我从德国人Rumm对此帖子的回复中了解到, 为什么UTF-16中使用jQuery POST that the problem was coming from escape(), but I'm not using it as you can see from my script below: 问题出在escape(),但我没有使用它,如下面的脚本所示:

<script>
$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var re = new XRegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
);
$(document).ready(function() {
    $("#xyzzyForm").validate({
        rules: {
            xyzzy: {
                required: true,
                minlength: 8,
                maxlength: 32,
                regex: '^[\\p{L}\\p{M}*+\\p{N}]*$'
            }
        },
        messages: {
            xyzzy: {
                required: "Please enter your xyzzy.",
                maxlength: "The xyzzy field only accepts between 8-32 characters.",
                minlength: "The xyzzy field only accepts between 8-32 characters.",
                regex: "The xyzzy field contains invalid characters.  This field only accepts letters and/or numbers."
            }
        },
        submitHandler: function(form) {
            var request;
            if (request) request.abort();
            var $inputs = $(form).find("input, select, textarea, button");
            var serializedData = $(form).serialize();
            $inputs.prop("disabled", true);
            request = $.ajax({
                url: "./xyzzy-ajax.php",
                cache: false,
                type: "post",
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data: serializedData
            });
            // Prevent default posting of form.
            return false;
        }
    });
});
</script>

Any idea where the re-encode to UTF-16 is occurring in this case? 您是否知道在这种情况下会在哪里重新编码为UTF-16?

Ok I figured out the problem, little embarrassed to say I was led astray but comments regarding JQuery UTF-8 content being encoded to UTF-16 in another post but that wasn't the problem. 好的,我弄清楚了这个问题,不好意思说我误入歧途,但是在另一篇文章中,有关将JQuery UTF-8内容编码为UTF-16的评论却不是问题。

Turns out the UTF-8 characters are simply being raw url encoded . 事实证明,UTF-8字符仅是原始url编码 All I need to do to decode them on my PHP side is run them through rawurldecode() first then pass it on to my filters for further testing. 我需要在PHP端对其进行解码的所有操作是先通过rawurldecode()运行它们,然后将其传递给我的过滤器以进行进一步测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM