AJAX 编码问题分析

1、 要发送的内容

格式: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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值