这里提供asp环境下采用gb2312编码,通过ajax技术的post发送方法提交数据时,对于特殊字符的乱码问题给予一个完美的解决方案——即发送数据利用encodeURIComponent和escape套接的方法——本实例经过测试,完全解决ajax关于在gb2312编码下post发送特殊符号所产生的乱码问题。
完整代码如下
- <script>
- varxmlhttp=getHTTPObject();
- varpost="大头爸爸测试字符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);
- returntrue;
- }
- }
- xmlhttp.send("act="+encodeURIComponent(escape(post)));//encodeURIComponent和escape套接的方法
- functiongetHTTPObject(){
- if(typeofXMLHttpRequest!='undefined')
- newXMLHttpRequest();
- try{
- newActiveXObject("Msxml2.XMLHTTP");
- }catch(e){
- try{
- newActiveXObject("Microsoft.XMLHTTP");
- }catch(e){}
- false;
- </script>