1、 要发送的内容:
格式:xml;编码:utf-8
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(request位设置编码)
结果:
1. //注意:tempContent输出未标明正常的,均为不正常
2.
3. log.debug("encoding=" + encoding); //encoding=utf-8
4.
5. log.debug("tempContent=" + tempContent); //正常
6. log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); //正常
7. log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); //正常
8. log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); //正常
1. </pre><pre>
下面代码为XML内容生成代码:
1. function makeXmlTemp() {
2. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
3. xmlDoc.async = false;
4.
5. var p = xmlDoc.createProcessingInstruction("xml","version=/"1.0/" encoding=/"utf-8/"");
6. xmlDoc.appendChild(p);
7. var root = xmlDoc.createElement('ocr');
8. xmlDoc.appendChild(root);
9.
10.
11. //alert(xmlDoc.xml);
12. var str = xmlDoc.xml;
13. str = str.replace('?>', ' encoding=/"utf-8/"?>');
14. return str;
下面代码为AJAX 发送代码:
view plaincopy to cppboardprint?
1. //AJAX请求,使用同步方法
2. function ajaxRequest(url, param, method){
3. var xmlHttp;
4. var rs;
5. var isie = true;
6.
7. if(window.ActiveXObject){
8. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
9. isie = true;
10. }else if(window.XMLHttpRequest){
11. xmlHttp = new XMLHttpRequest();
12. }
13. try{
14. if(isie == false ){
15. xmlHttp.open("GET", url, false);
16. xmlHttp.overrideMimeType("text/html;charset=gb2312");
17. xmlHttp.send(null);
18. //alert(xmlHttp.responseText);
19. alert("只支持IE!");
20. }else{
21. if(method == 'POST'){
22. xmlHttp.open("POST", url, false);
23. xmlHttp.setRequestHeader("Content-Type","apppcation/x-www-form-urlencoded; charset=UTF-8");
24. xmlHttp.send(param);
25. }else{
26. xmlHttp.open("GET", url, false);
27. xmlHttp.send(null);
28. }
29.
30. if(xmlHttp.readyState == 4){
31. if (xmlHttp.status == 200 || xmlHttp.status == 0){
32. return xmlHttp.responseText;
33. }
34. }
35. }
36. }catch(exception){
37. alert('exception!');
38. //alert('exception:'+exception.message);
39. }
40. }
41.
42.
43. var tempDat = makeXmlTemp();
44. //alert('tempDat=' + tempDat);
45.
46.
47. $i('txtTempFields').value = tempDat;
48.
49.
50. var tempUrl = url.getURI();
51. alert(tempUrl);
52. var params = 'tempName=' + tempName + '&tempContent=' + encodeURIComponent(tempDat);
53. var echo = ajaxRequest(tempUrl, params,'POST');
54. alert(echo);
下面为servlet 代码:
1. void doPost(HttpServletRequest req, HttpServletResponse resp)
2. throws ServletException, IOException {
3. String encoding = req.getCharacterEncoding();
4. log.debug("encoding=" + encoding);
5. //req.setCharacterEncoding("utf-8");
6. try {
7. String tempName = req.getParameter("tempName");
8. String tempContent = req.getParameter("tempContent");
9.
10. String rv = SUCCESS;
11.
12. log.debug("tempName=" + tempName);
13.
14. log.debug("tempContent=" + tempContent);
15.
16. log.debug("1tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"gb2312"));
17. log.debug("2tempContent=" + new String(tempContent.getBytes("UTF-8"),"gb2312"));
18. log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312"));
19.
20. log.debug("4tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"UTF-8"));
21. log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8"));
22. log.debug("6tempContent=" + new String(tempContent.getBytes("gb2312"),"UTF-8"));
23.
24. log.debug("7tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"ISO8859-1"));
25. log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1"));
26. log.debug("9tempContent=" + new String(tempContent.getBytes("gb2312"),"ISO8859-1"));
27.
28. tempContent = URLDecoder.decode(tempContent,"UTF-8");
29. log.debug("8tempContent=" +URLDecoder.decode(tempContent,"UTF-8"));}
30. catch (Exception e) {
31. e.printStackTrace();log.error("savaTempData failed.", e);
32. }
33. }
2、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(reqest未设置编码)
结果:
1. log.debug("encoding=" + encoding); //encoding=utf-8
2.
3. log.debug("tempContent=" + tempContent); //正常
4. log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); //正常
5. log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); //正常
6. log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); //正常
可以看出后台能正确解码与XML的编码无关。
3、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:位设置(req.getCharacterEncoding();读出客户端编码为null)
1. xmlHttp.setRequestHeader("Content-Type","apppcation/x-www-form-urlencoded");
servlet
编码:缺省(reqest未设置编码)
结果:
log.debug("encoding=" + encoding); //encoding=null
log.debug("4tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"UTF-8")); //正常
4、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:位设置(req.getCharacterEncoding();读出客户端编码为null)
1. xmlHttp.setRequestHeader("Content-Type","apppcation/x-www-form-urlencoded;);
servlet
编码:
1. req.setCharacterEncoding("utf-8"); //或者使用下面的语句
2. //req.setCharacterEncoding("ISO8859-1");
结果:
与3相同。可见,req指定编码并不能正常输出,需要转码。并且和使用encodeURIComponent()与否无关(使用一次)。
5、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:gbk(req.getCharacterEncoding();读出客户端编码为gbk)
1. xmlHttp.setRequestHeader("Content-Type","apppcation/x-www-form-urlencoded");
servlet
编码:
1. req.setCharacterEncoding("utf-8"); //或者使用下面的语句
2. //req.setCharacterEncoding("ISO8859-1");
结果:
均不能正常解码。
总结
通过实验可以看出,AJAX post数据的编码和数据本身无关,和SERVLET是否设置编码无关:
req.setCharacterEncoding("utf-8")
仅和AJAX使用的编码有关,并且只能是utf-8(不是utf-8有可能吗?):
xmlHttp.setRequestHeader("Content-Type","apppcation/x-www-form-urlencoded; charset=UTF-8");
若自己封装AJAX函数时,不要忘记指定字符集属性:charset=utf-8
评分:0
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。