我正在构建一个表单,它使用带有 XRegexp 的 JQuery.Validator 在将输入字段提交到服务器之前清理它们(这也验证所有传入的帖子),但是我在将内容发布回服务器时遇到了一些麻烦。
如果我发布这样的 UTF-8 字符串:Русский日本语fdsffsfsa我的 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
我从 German Rumm 对这篇文章的回复中知道为什么 jQuery POSTing 使用 UTF-16?问题来自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>
知道在这种情况下发生在哪里重新编码为 UTF-16 吗?