ajax/post/gb2312特殊字符出现乱码完美解决方法
2012-08-30 13:24
603 查看
这里提供asp环境下采用gb2312编码,通过ajax技术的post发送方法提交数据时,对于特殊字符的乱码问题给予一个完美的解决方案——即发送数据利用encodeURIComponent和escape套接的方法——本实例经过测试,完全解决ajax关于在gb2312编码下post发送特殊符号所产生的乱码问题。
完整代码如下
服务端测试代码如下(以ASP为例)
测试结果
测试结果:测试结果表明,采用本方法完全解决了asp/ajax/post/gb2312特殊字符出现乱码问题。
另外在附带说明一下:
上面的方法中经测试符号“<”、“>”、“ ”(空格)、“回车换行”等符号是不被转换的,其它的符号,比如连接符(&)会转换为&;单引号(')会转换为';双引号(")会转换为"等等。一般通过采用textarea表单来提供输入容器,因此如果提交的信息中不支持html的话——其实在一般场合是不能支持html代码的,因此要encodeURIComponent(escape(msg))无法编码的字符进行手工编码。还好这类编码字符并不多,我测试的这些字符有:<、>、空格、回车换行等。因此对这些字符需要做如下处理:
测试一段代码
msg=msg.HtmlTextEncode();
msg=encodeURIComponent(escape(msg));//此行必须在上一行的下面
以上面代码测试符号<
执行完:msg=msg.HtmlTextEncode();后,msg的值为<
执行完:msg=encodeURIComponent(escape(msg)); msg的值为%2526lt%253B
在后台:Response.Write Unescape(msg)取得的值为<
存入数据库的值为:<
同理,如果在textarea框中输入
存入数据库的值是
读取数据库前台显示的值是
如果按下面顺序执行,同样测试符号<
执行完:msg=encodeURIComponent(escape(msg)); msg的值为%253C
执行完:msg=msg.HtmlTextEncode();后,msg的值为%253C
在后台:Response.Write Unescape(msg)取得的值为<
存入数据库的值为:<
同理,如果在textarea框中输入
存入数据库的值是
读取数据库前台显示的值是
完整代码如下
<script> var xmlhttp=getHTTPObject(); var post="大头爸爸测试字符www.asp163.org!@#$%^&*()_+|!·#¥%……—*()——+|§№☆★○●◎◇◆□〓↓↑←→※▲△■#&@\^_ ̄○㈡【】()[]{}〗〖"; xmlhttp.open('POST','more/file277/test.asp?random='+Math.random(), true); xmlhttp.setrequestheader("content-length",post.length); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4&&xmlhttp.status == 200){ alert(xmlhttp.responseText); return true; } } xmlhttp.send("act="+encodeURIComponent(escape(post)));//encodeURIComponent和escape套接的方法 function getHTTPObject(){ if(typeof XMLHttpRequest!='undefined') return new XMLHttpRequest(); try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } return false; } </script>
服务端测试代码如下(以ASP为例)
<% Response.CodePage=936 Response.Charset="gb2312" Response.Write Unescape(Request("act")) %>
测试结果
大头爸爸测试字符www.asp163.org!@#$%^&*()_+|!·#¥%……—*()——+|§№☆★○●◎◇◆□〓↓↑←→※▲△■#&@\^_ ̄○㈡【】()[]{}〗〖
测试结果:测试结果表明,采用本方法完全解决了asp/ajax/post/gb2312特殊字符出现乱码问题。
<script> var xmlhttp=getHTTPObject(); var post="大头爸爸测试字符www.asp163.org!@#$%^&*()_+|!·#¥%……—*()——+|§№☆★○●◎◇◆□〓↓↑←→※▲△■#&@\^_ ̄○㈡【】()[]{}〗〖"; xmlhttp.open('POST','more/file277/test.asp?random='+Math.random(), true); xmlhttp.setrequestheader("content-length",post.length); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4&&xmlhttp.status == 200){ alert(xmlhttp.responseText); return true; } } xmlhttp.send("act="+encodeURIComponent(escape(post)));//encodeURIComponent和escape套接的方法 function getHTTPObject(){ if(typeof XMLHttpRequest!='undefined') return new XMLHttpRequest(); try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } return false; } </script>
另外在附带说明一下:
上面的方法中经测试符号“<”、“>”、“ ”(空格)、“回车换行”等符号是不被转换的,其它的符号,比如连接符(&)会转换为&;单引号(')会转换为';双引号(")会转换为"等等。一般通过采用textarea表单来提供输入容器,因此如果提交的信息中不支持html的话——其实在一般场合是不能支持html代码的,因此要encodeURIComponent(escape(msg))无法编码的字符进行手工编码。还好这类编码字符并不多,我测试的这些字符有:<、>、空格、回车换行等。因此对这些字符需要做如下处理:
//文本: 格式化字符 //msg是收集的textarea内容 msg=msg.delSpace(); msg=msg.HtmlTextEncode(); msg=encodeURIComponent(escape(msg)); //删除行末空格,删除文章结尾的空行、空格 ->写入到数据库 function String.prototype.delSpace(){ var tmpstr=this; tmpstr=tmpstr.replace(/( )+\r/g,'\r');//删除行末空格 tmpstr=tmpstr.replace(/[\r\n]+( )*[\r\n]*$/g,'');//删除文章结尾的空行、空格 return tmpstr; } //HTML部分代码转码 从TextArea写入到数据库 function String.prototype.HtmlTextEncode(){ var tmpstr=this; tmpstr=tmpstr.replace(/</g,"<");//替换<号 tmpstr=tmpstr.replace(/>/g,">");//替换>号 tmpstr=tmpstr.replace(/\r\n/g,"<br\/>");//替换\n tmpstr=tmpstr.replace(/ /g," "); return tmpstr; }
测试一段代码
msg=msg.HtmlTextEncode();
msg=encodeURIComponent(escape(msg));//此行必须在上一行的下面
以上面代码测试符号<
执行完:msg=msg.HtmlTextEncode();后,msg的值为<
执行完:msg=encodeURIComponent(escape(msg)); msg的值为%2526lt%253B
在后台:Response.Write Unescape(msg)取得的值为<
存入数据库的值为:<
同理,如果在textarea框中输入
a b
存入数据库的值是
a<br/>b
读取数据库前台显示的值是
a b
如果按下面顺序执行,同样测试符号<
执行完:msg=encodeURIComponent(escape(msg)); msg的值为%253C
执行完:msg=msg.HtmlTextEncode();后,msg的值为%253C
在后台:Response.Write Unescape(msg)取得的值为<
存入数据库的值为:<
同理,如果在textarea框中输入
a b
存入数据库的值是
a b
读取数据库前台显示的值是
a b
相关文章推荐
- ajax/post/gb2312特殊字符出现乱码完美解决方法
- JQuery的Ajax中Post方法传递中文出现乱码的解决方法
- XML中特殊字符出现乱码的解决方法
- JQuery的Ajax中Post方法传递中文出现乱码的解决方法
- ajax post 提交中文java后台获取参数出现乱码解决方法
- AJAX利用javascript向后台传递中文字符出现乱码解决方法。
- AJAX在ResponseText后出现乱码的解决方法
- AJAX POST数据中有特殊符号导致数据丢失的解决方法
- VB6中剪切复制至剪贴板,粘贴时中文出现乱码的原因及完美解决方法
- 两种方法解决javascript url post 特殊字符转义 + & #
- jquery的ajax向ashx传中文,出现乱码的解决方法
- 通过ajax传值,出现的乱码和特殊字符问题
- ajax传送参数含有特殊字符的快速解决方法
- SpringMVC学习—使用ajax返回Json出现乱码解决方法
- 怎么使用iconv来解决ajax用post传递过去出现的乱码
- ajax post 提交数据到服务端时中文乱码解决方法
- VC轻松解析XML文件--CMarkup使用方法(解决解析中文字符出现乱码问题)
- jQuery调用AJAX时Get和post公用的乱码解决方法
- js调用AJAX时Get和post的乱码解决方法