当通过此ajax函数发送带有日语字符的表单时,字符将以日语格式发送到服务器,并且数据在数据库中存储为¿。
var strAction = "/_ajax/save/"+sSavePage+"?action=saveseo&intFolderID="+iFolderID+"&intPageID="+iPageID;
var frm = $("#frmSmartPage");
var data = frm.serialize();
$.ajax({
type: frm.attr('method'),
url: strAction,
data: data,
success: function (data) {
alert('ok');
}
});
在同一页面上,还可以通过提交来发布表单。然后日语字符转换为 &#<number>
格式。
<form method="post" target="ajax_save" autocomplete="off" name="frmSmartPage" id="frmSmartPage" action="<%=constBetaPath%>/_ajax/save/pages_save.asp?intPageID=<%=intPageID%>&intFolderID=<%=intFolderID%>&action=save" onSubmit="return validateSave()">
我希望能够将日语字符转换为 &#<number>
在 ajax 调用中格式化,但到目前为止我还没有任何运气。
我已经尝试过的事情:
var data = unescape(encodeURIComponent(frm.serialize()));
---
var data = escape(frm.serialize());
---
accepts: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
---
contentType: 'application/x-www-form-urlencoded;'
---
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
编辑:
HTML 编码:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
编辑2:
后端代码正在将 iso-8859-1 解码为 UTF8
'******************************************************************************************************************
'' @SDESCRIPTION: Decodes from ISO-8859-1 to UTF8
'' @PARAM: - s [string]: your string to be decoded
'' @RETURN: [string] decoded string
'' @DESCRIPTION: Usefull to use when saving special chars from a ISO-8859-1 post to an UTF-8 page, example via AJAX
'******************************************************************************************************************
public function DecodeUTF8(s)
dim i
dim c
dim n
s = s + " "
i = 1
do while i <= len(s)
c = asc(mid(s,i,1))
if c and &H80 then
n = 1
do while i + n < len(s)
if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then
exit do
end if
n = n + 1
loop
if n = 2 and ((c and &HE0) = &HC0) then
c = asc(mid(s,i+1,1)) + &H40 * (c and &H01)
else
c = 191
end if
s = left(s,i-1) + chr(c) + mid(s,i+n)
end if
i = i + 1
loop
DecodeUTF8 = Left(s, Len(s)-1)
end function
解决方案 感谢 Álvaro González 的回复,我能够通过创建用于提交的临时表单来创建解决方法。
var strAction = "/_ajax/save/"+sSavePage+"?action=saveseo&intFolderID="+iFolderID+"&intPageID="+iPageID;
var newForm = $('<form />');
var orginalForm = $("#frmSmartPage");
newForm.append(orginalForm.clone().children());
newForm.attr('method', 'post');
newForm.attr('target', 'ajax_save');
newForm.attr('action', strAction);
newForm.css('display', 'none');
orginalForm.parent().append(newForm);
var target = $("#ajax_save");
target.one('load', function () {
newForm.remove();
});
newForm.submit();
最佳答案
您遇到了一个严重的根本问题:ISO-8859-1 字符集(也称为 Latin-1,应该已经给您提供了线索)是为西欧语言使用的拉丁文字设计的,根本无法对日语进行编码人物。在其他地方,您都使用 UTF-8,这是迄今为止唯一合理的编码选择,并且没有任何此类限制,但 ISO-8859-1 是链条中的薄弱环节,使一切变得非常复杂。
更糟糕的是,我发现了一些令我担心的细节。您正在使用 AJAX 发送信息,并且从 AJAX mandates UTF-8 开始,jQuery 会自动将其转换为 UTF-8。但是,服务器端代码错误地假定 ISO-8859-1,并且会进行虚假转换。如果此代码已在生产中,则它可能会损坏您已有的数据。
你基本上有两个选择:
将所有内容切换为 UTF-8。这将为您节省将来的所有编码问题,但需要仔细迁移当前代码库。
找出一种在客户端代码中将日语编码为 ISO-8859-1 并在服务器端代码中正确解码的方法。值得庆幸的是,浏览器已经意识到这个问题,并且(因为 HTML 是它们的主语言)当它们必须提交包含不支持的字符的表单时,它们通常决定使用 HTML 实体(这就是那些
&#<number>
的来源)通过文档编码。在这种情况下,您需要做的是将服务器端代码更改为:
- 不进行任何编码转换(数据已经是 UTF-8)
- 解码 HTML 实体(考虑到字符串为 UTF-8)
关于javascript - 将日语实体转换为 HTML 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869237/