AJAX 编码问题分析
2011-09-19 14:16
253 查看
1、 要发送的内容:
格式:xml;编码:utf-8
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(request位设置编码)
结果:
下面代码为XML内容生成代码:
下面代码为AJAX 发送代码:
下面为servlet 代码:
2、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(reqest未设置编码)
结果:
可以看出后台能正确解码与XML的编码无关。
3、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:位设置(req.getCharacterEncoding();读出客户端编码为null)
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)
servlet
编码:
结果:
与3相同。可见,req指定编码并不能正常输出,需要转码。并且和使用encodeURIComponent()与否无关(使用一次)。
5、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:gbk(req.getCharacterEncoding();读出客户端编码为gbk)
servlet
编码:
结果:
均不能正常解码。
总结
通过实验可以看出,AJAX post数据的编码和数据本身无关,和SERVLET是否设置编码无关:
req.setCharacterEncoding("utf-8")
仅和AJAX使用的编码有关,并且只能是utf-8(不是utf-8有可能吗?):
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
若自己封装AJAX函数时,不要忘记指定字符集属性:charset=utf-8
格式:xml;编码:utf-8
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(request位设置编码)
结果:
//注意:tempContent输出未标明正常的,均为不正常 log.debug("encoding=" + encoding); //encoding=utf-8 log.debug("tempContent=" + tempContent); //正常 log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); //正常 log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); //正常 log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); //正常
下面代码为XML内容生成代码:
function makeXmlTemp() { var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; var p = xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\""); xmlDoc.appendChild(p); var root = xmlDoc.createElement('ocr'); xmlDoc.appendChild(root); //alert(xmlDoc.xml); var str = xmlDoc.xml; str = str.replace('?>', ' encoding=\"utf-8\"?>'); return str; }
下面代码为AJAX 发送代码:
//AJAX请求,使用同步方法 function ajaxRequest(url, param, method){ var xmlHttp; var rs; var isie = true; if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); isie = true; }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } try{ if(isie == false ){ xmlHttp.open("GET", url, false); xmlHttp.overrideMimeType("text/html;charset=gb2312"); xmlHttp.send(null); //alert(xmlHttp.responseText); alert("只支持IE!"); }else{ if(method == 'POST'){ xmlHttp.open("POST", url, false); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); xmlHttp.send(param); }else{ xmlHttp.open("GET", url, false); xmlHttp.send(null); } if(xmlHttp.readyState == 4){ if (xmlHttp.status == 200 || xmlHttp.status == 0){ return xmlHttp.responseText; } } } }catch(exception){ alert('exception!'); //alert('exception:'+exception.message); } } var tempDat = makeXmlTemp(); //alert('tempDat=' + tempDat); $i('txtTempFields').value = tempDat; var tempUrl = url.getURI(); alert(tempUrl); var params = 'tempName=' + tempName + '&tempContent=' + encodeURIComponent(tempDat); var echo = ajaxRequest(tempUrl, params,'POST'); alert(echo);
下面为servlet 代码:
void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String encoding = req.getCharacterEncoding(); log.debug("encoding=" + encoding); //req.setCharacterEncoding("utf-8"); try { String tempName = req.getParameter("tempName"); String tempContent = req.getParameter("tempContent"); String rv = SUCCESS; log.debug("tempName=" + tempName); log.debug("tempContent=" + tempContent); log.debug("1tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"gb2312")); log.debug("2tempContent=" + new String(tempContent.getBytes("UTF-8"),"gb2312")); log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); log.debug("4tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"UTF-8")); log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); log.debug("6tempContent=" + new String(tempContent.getBytes("gb2312"),"UTF-8")); log.debug("7tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"ISO8859-1")); log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); log.debug("9tempContent=" + new String(tempContent.getBytes("gb2312"),"ISO8859-1")); tempContent = URLDecoder.decode(tempContent,"UTF-8"); log.debug("8tempContent=" +URLDecoder.decode(tempContent,"UTF-8"));} catch (Exception e) { e.printStackTrace();log.error("savaTempData failed.", e); } }
2、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:utf-8(req.getCharacterEncoding();读出客户端编码为utf-8)
servlet
编码:缺省(reqest未设置编码)
结果:
log.debug("encoding=" + encoding); //encoding=utf-8 log.debug("tempContent=" + tempContent); //正常 log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); //正常 log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); //正常 log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); //正常
可以看出后台能正确解码与XML的编码无关。
3、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:位设置(req.getCharacterEncoding();读出客户端编码为null)
xmlHttp.setRequestHeader("Content-Type","application/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)
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;);
servlet
编码:
req.setCharacterEncoding("utf-8"); //或者使用下面的语句 //req.setCharacterEncoding("ISO8859-1");
结果:
与3相同。可见,req指定编码并不能正常输出,需要转码。并且和使用encodeURIComponent()与否无关(使用一次)。
5、 要发送的内容:
格式:xml;编码:gb2312
AJAX
编码:gbk(req.getCharacterEncoding();读出客户端编码为gbk)
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
servlet
编码:
req.setCharacterEncoding("utf-8"); //或者使用下面的语句 //req.setCharacterEncoding("ISO8859-1");
结果:
均不能正常解码。
总结
通过实验可以看出,AJAX post数据的编码和数据本身无关,和SERVLET是否设置编码无关:
req.setCharacterEncoding("utf-8")
仅和AJAX使用的编码有关,并且只能是utf-8(不是utf-8有可能吗?):
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
若自己封装AJAX函数时,不要忘记指定字符集属性:charset=utf-8
相关文章推荐
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 深入分析 Java 中的中文编码问题
- 从ffmpeg源代码分析如何解决ffmpeg编码的延迟问题 (转)
- Ajax发送请求的编码问题
- 深入分析 Java 中的中文编码问题
- 深入分析 Java 中的中文编码问题
- ASP.NET基于Ajax的Enter键提交问题分析
- 深入分析 Java 中的中文编码问题(1)
- AJAX Unit01: Ajax 、 编码问题
- 从ffmpeg源代码分析如何解决ffmpeg编码的延迟问题
- 深入分析Java中的编码问题
- 深入分析 Java 中的中文编码问题
- 深入分析 Java 中的中文编码问题
- 深入分析 Java 中的中文编码问题——乱码是如何产生的
- ajax缓存和编码问题
- Ajax执行顺序流程及回调问题分析
- JSP编码及AJAX的调用问题
- 深入分析 Java 中的中文编码问题
- 深入分析 Java 中的中文编码问题
- 深入分析Java 中的中文编码问题