AJAX传送中文会导致乱码的问题

来源:岁月联盟 编辑:exp 时间:2011-09-10


使用POST的时候: 
//如果传送参数是直接赋予的,就会产生乱码! 
http_request.open("POST",url,true);  
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=gb2312');  
http_request.send("action="+strName+"&val="+val);   //如果val的值为中文,则产生乱码 
//解决方法很简单:使用javascript中的escape(string) 函数 
http_request.open("POST",url,true);  
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=gb2312');  
http_request.send("action="+strName+"&val="+escape(val));   //val的值为中文不会产生乱码 
 
使用GET的时候: 
1、在html标签meta中加入content="text/html; charset=gb2312" 确认浏览器解析时的编码. 
2、确认服务器层面上的编码方式 
JSP:response.setHeader("Charset","GB2312"); 
 
作者“ERDP技术架构”