2

While processing a huge XML client-side, got stuck with the following issue: some unicode characters are replaced with unreadable sequences, so server cannot parse that XML. Testing like this:

var text = new XMLSerializer().serializeToString(xmlNode);
console.log(text);
var req = new XMLHttpRequest();
req.open('POST', config.saveUrl, true);
req.overrideMimeType("application/xml; charset=UTF-8");
req.send(text);

Logging still shows the correct string:

<Language Self="Language/$ID/Czech" Name="$ID/Czech" SingleQuotes="‚‘" DoubleQuotes="„“" PrimaryLanguageName="$ID/Czech" SublanguageName="$ID/" Id="266" HyphenationVendor="Hunspell" SpellingVendor="Hunspell" />

While in the request (Chrome dev tools) and at server side it appears modified like this:

<Language Self="Language/$ID/Czech" Name="$ID/Czech" SingleQuotes="‚‘" DoubleQuotes="„“" PrimaryLanguageName="$ID/Czech" SublanguageName="$ID/" Id="266" HyphenationVendor="Hunspell" SpellingVendor="Hunspell" />

Original encoding of the XML file is UTF-8, too. Absolutely the same behavior when using jQuery.

4

2 回答 2

0
  1. 检查 overrideMimeType 使用大写“UTF-8”或小写“utf-8”
  2. 确保 javascript 计算之前的字符串在 utf-8 中(检查页面字符集)
  3. 在将其发送到服务器之前使用 escape/encodeURIComponent/decodeURIComponent 并在服务器上对其进行转义
  4. 尝试 application/x-www-form-urlencoded ans 像纯文本一样发送 xml

PS 修改后的字符串在 ISO-8859-15 中

于 2013-07-01T13:15:51.093 回答
0

它似乎这样做了。

我这里有数据 json 参数,其中包含一个字符串“Lääke”(芬兰语),我通过 ajax 发送到服务器。

这不起作用,服务器应用程序没有收到“ää”而是“??”:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState === 4 && this.status === 200) {
    status = this.responseText;
    if (status === "OK") {
        window.location.assign("ackok.html");
    }
    else {
        window.location.assign("ackerror.html");
    }
  }
};
xhttp.open("POST", "ProcessOrderServlet?Action=new&Customer="+data, true);    
xhttp.send();

这确实有效,服务器收到'ää':

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState === 4 && this.status === 200) {
    status = this.responseText;
    if (status === "OK") {
        window.location.assign("ackok.html");
    }
    else {
        //orderStatusElement[0].innerHTML = "<b>Palvelimella jokin meni vikaan. Yritä myöhemmin uudelleen </b>";
        window.location.assign("ackerror.html");
    }
  }
};
xhttp.open("POST", "ProcessOrderServlet?Action=new&Customer="+data, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
xhttp.send(); 
于 2017-08-21T13:12:06.197 回答