您的位置:首页 > 其它

ajax/post/gb2312特殊字符出现乱码完美解决方法

2012-08-30 13:24 555 查看
这里提供asp环境下采用gb2312编码,通过ajax技术的post发送方法提交数据时,对于特殊字符的乱码问题给予一个完美的解决方案——即发送数据利用encodeURIComponent和escape套接的方法——本实例经过测试,完全解决ajax关于在gb2312编码下post发送特殊符号所产生的乱码问题。

完整代码如下

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: