使用Ajax时的Chartset编码?JQuery的

Art*_*ald 12 ajax jquery character-encoding

我有一个Web应用程序(UTF-8),其中可以使用以下一个发送到服务器端

áéíóú
àèìòù
ÀÈÌÒÙ
ÁÉÍÓÚ
Run Code Online (Sandbox Code Playgroud)

好.我使用类似如下的内容来发送数据

// Notice $("#myForm").serialize()
$.get("/path?", $("#myForm").serialize(), function(response) {

});
Run Code Online (Sandbox Code Playgroud)

当我看到我的recordSet时,我得到(数据库charSet编码为UTF-8)

áéíóú
à èìòù
ÃÉÃÓÚ
ÀÈÌÒÙ
Run Code Online (Sandbox Code Playgroud)

即使使用$ .post,我也会获得相同的结果集

在Action Book中看到JQuery中的serialize()方法之后:

从包装集中的所有成功表单元素创建格式正确且编码的查询字符串

但是,如上所示,它似乎没有正常工作.因此,我使用而不是serialize()方法

var objectArray =  $("#myForm").serializeArray();

var queryString = "";
for(var i = 0; i < objectArray.length; i++) {
    queryString += "&" + objectArray[i]["name"] + "=" + objectArray[i]["value"];
}

$.get("/path?" + queryString, null, function(response) {

});
Run Code Online (Sandbox Code Playgroud)

现在我进入数据库

áéíóú
àèìòù
ÀÈÌÒÙ
ÁÉÍÓÚ
Run Code Online (Sandbox Code Playgroud)

所以我在使用serialize()方法时错过了什么?为什么serialize()方法不能按预期工作?

小智 5

我使用以下行在PHP中解决它:

foreach($_POST as $key => $value) {
    $_POST[$key] = utf8_decode($value);
}
Run Code Online (Sandbox Code Playgroud)


and*_*lzo 3

jQuery.getencodeURIComponent就像 jQuery ajax 用于数据的所有函数一样。如果您通过 GET 发送数据,XHR 会自动负责传递 URL 编码。您可以查看 的文档,encode了解哪种类型的编码适合您发送的数据