`
tomEnjoy
  • 浏览: 145742 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

AJAX POST时的中文乱码问题和解决

    博客分类:
  • Ext
阅读更多
问题描述:
       我在js中建立一个数组items,如下:items=[{name:'中文'}],我把这个数组转换成字符串发往后台,后台接收数据并存入数据库:
js 代码
 
  1. Ext.Ajax.request({  
  2.         url:'',  
  3.         params:{  
  4.             items:Ext.util.JSON.encode(items)  
  5.         },  
  6.         method:"POST",  
  7.         success:function(){alert("成功")}  
  8. });  
结果这样传送的字符串不能正确的得到解析,无论在firefox下观察params的串还是后台接收的数据都是乱码。于是我改了下代码:
js 代码
 
  1. Ext.Ajax.request({  
  2.         url:'',  
  3.         params:{  
  4.             items:encode(Ext.util.JSON.encode(items))   //就是将字符串编码一下  
  5.         },  
  6.         method:"POST",  
  7.         success:function(){alert("成功")}  
  8. });  
这样,传输的字符串是以utf-8的编码方式。
然而,这样在后台接收数据并存储到数据库仍然是乱码的形式,我后台全部都是以UTF-8编码方式。
解决:
        试了好长时间,最后终于解决,方法很简单,在后台加入这样一行代码:
java 代码
 
  1. String str=req.getParameter("items");  
  2. str=java.net.URLDecoder.decode(str, "UTF-8");  
这行代码的意思是把得到的application/x-www-form-urlencoded 编码方式的字符串解码成utf-8编码方式。

请教:(虽然在网上找到很多关于乱码问题的解决办法,但大都不能针对自己的问题。)
       虽然已经解决了乱码问题,但实际上我还不是很明白在js不进行encode而直接传送为什么会是乱码,难道Ext的Ajax不是默认UTF-8编码方式?。请大家指教!!!
            
分享到:
评论
10 楼 neoliao 2008-03-05  
在服务器端使用filter
9 楼 hax 2008-02-18  
真是老生常谈了,在2001年混CSDN的时候我就到处靠传播点encoding的基本常识来接分,7年过去了,抗战都要胜利了,咋还有人不断的栽倒在这上面呢?
8 楼 InnocentBoy 2008-02-18  
我的问题 到现在还没解决。也是乱码问题。我用的prototype
7 楼 boogie 2008-01-24  
在Request Headers中设置Content-Type:application/x-www-form-urlencoded; charset=utf-8,具体方法:将Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8'加在Ext.onReady块里,此方法可以解决用EXT在POST时的中文乱码问题。
6 楼 zjumty 2007-12-24  
如果是做Ajax应用程序, 最好是整体上采用utf-8编码.

回楼主, Ext是用UTF-8的, 是你的服务端没有采用UTF-8.
5 楼 tomEnjoy 2007-12-24  
看到了以下这段话:
碰到使用ajax获得数据,或者提交数据出现乱码
英文情况下不会出现乱码,用了中文才可能乱掉,这是因为咱们的win操作系统,保存文件的默认编码是gb2312,而ajax传输数据的默认编码是utf-8,推荐大家将数据格式统一为utf-8,不但可以解决眼前的乱码问题,对以后扩展为多语言也有好处。
4 楼 bigxxs 2007-12-21  
客户端文件本身的存储格式是否UTF-8格式?
3 楼 mssnzxm 2007-12-21  
客户端: items:Ext.util.JSON.encode(items)
服务端这样:
strJsonData = new String(strJsonData.trim().getBytes("ISO-8859-1"), "utf-8");

就OK了
2 楼 ajaxgo 2007-12-14  
escpe并不好,用encodeURIComponent
1 楼 xiaocheng 2007-12-13  
<p><font>我的博客里有一个解决方案<font><a href='http://xiaocheng.iteye.com/blog/147913'>http://xiaocheng.iteye.com/blog/147913</a></font></font></p>
<p><font>解决方案是用escape函数来编码.<br/>
比如<br/>
var peoName="张三";<br/>
    peoName=escape(escape(peoName));</font><font><br/>
    //看到为什么两次用到escape函数不要奇怪.<br/>
这样提交到服务器后,我们只要用一个java方法来解码就可以了.<br/>
action中我们只要用Escape.java中的unescape静态方法就可解码。<br/>
</font><font><font>String Name= Escape.unescape(request.getParameter("peoName")).trim();</font></font></p>
<div class='code_title'>Escape.java  代码</div>
<div class='dp-highlighter'>
<div class='bar'/>
<ol class='dp-j'>
    <li class='alt'><span><span class='keyword'>package</span><span> com.ctgusec.oaPlus;       </span></span></li>
    <li class=''><span/><span class='comment'>/**    </span> </li>
    <li class='alt'><span><span class='comment'> * 将url中文参数编码或者反编码    </span> </span></li>
    <li class=''><span><span class='comment'> * javascript escape/unescape 编码的 Java 实现    </span> </span></li>
    <li class='alt'><span><span class='comment'> * author jackyz    </span> </span></li>
    <li class=''><span><span class='comment'> * keep this copyright info while using this method by free    </span> </span></li>
    <li class='alt'><span><span class='comment'> */</span><span>      </span></span></li>
    <li class=''><span/><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> Escape{       </span></li>
    <li class='alt'><span>    </span><span class='keyword'>private</span><span> </span><span class='keyword'>final</span><span> </span><span class='keyword'>static</span><span> String[] hex = {       </span></li>
    <li class=''><span>        </span><span class='string'>"00"</span><span>,</span><span class='string'>"01"</span><span>,</span><span class='string'>"02"</span><span>,</span><span class='string'>"03"</span><span>,</span><span class='string'>"04"</span><span>,</span><span class='string'>"05"</span><span>,</span><span class='string'>"06"</span><span>,</span><span class='string'>"07"</span><span>,</span><span class='string'>"08"</span><span>,</span><span class='string'>"09"</span><span>,</span><span class='string'>"0A"</span><span>,</span><span class='string'>"0B"</span><span>,</span><span class='string'>"0C"</span><span>,</span><span class='string'>"0D"</span><span>,</span><span class='string'>"0E"</span><span>,</span><span class='string'>"0F"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"10"</span><span>,</span><span class='string'>"11"</span><span>,</span><span class='string'>"12"</span><span>,</span><span class='string'>"13"</span><span>,</span><span class='string'>"14"</span><span>,</span><span class='string'>"15"</span><span>,</span><span class='string'>"16"</span><span>,</span><span class='string'>"17"</span><span>,</span><span class='string'>"18"</span><span>,</span><span class='string'>"19"</span><span>,</span><span class='string'>"1A"</span><span>,</span><span class='string'>"1B"</span><span>,</span><span class='string'>"1C"</span><span>,</span><span class='string'>"1D"</span><span>,</span><span class='string'>"1E"</span><span>,</span><span class='string'>"1F"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"20"</span><span>,</span><span class='string'>"21"</span><span>,</span><span class='string'>"22"</span><span>,</span><span class='string'>"23"</span><span>,</span><span class='string'>"24"</span><span>,</span><span class='string'>"25"</span><span>,</span><span class='string'>"26"</span><span>,</span><span class='string'>"27"</span><span>,</span><span class='string'>"28"</span><span>,</span><span class='string'>"29"</span><span>,</span><span class='string'>"2A"</span><span>,</span><span class='string'>"2B"</span><span>,</span><span class='string'>"2C"</span><span>,</span><span class='string'>"2D"</span><span>,</span><span class='string'>"2E"</span><span>,</span><span class='string'>"2F"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"30"</span><span>,</span><span class='string'>"31"</span><span>,</span><span class='string'>"32"</span><span>,</span><span class='string'>"33"</span><span>,</span><span class='string'>"34"</span><span>,</span><span class='string'>"35"</span><span>,</span><span class='string'>"36"</span><span>,</span><span class='string'>"37"</span><span>,</span><span class='string'>"38"</span><span>,</span><span class='string'>"39"</span><span>,</span><span class='string'>"3A"</span><span>,</span><span class='string'>"3B"</span><span>,</span><span class='string'>"3C"</span><span>,</span><span class='string'>"3D"</span><span>,</span><span class='string'>"3E"</span><span>,</span><span class='string'>"3F"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"40"</span><span>,</span><span class='string'>"41"</span><span>,</span><span class='string'>"42"</span><span>,</span><span class='string'>"43"</span><span>,</span><span class='string'>"44"</span><span>,</span><span class='string'>"45"</span><span>,</span><span class='string'>"46"</span><span>,</span><span class='string'>"47"</span><span>,</span><span class='string'>"48"</span><span>,</span><span class='string'>"49"</span><span>,</span><span class='string'>"4A"</span><span>,</span><span class='string'>"4B"</span><span>,</span><span class='string'>"4C"</span><span>,</span><span class='string'>"4D"</span><span>,</span><span class='string'>"4E"</span><span>,</span><span class='string'>"4F"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"50"</span><span>,</span><span class='string'>"51"</span><span>,</span><span class='string'>"52"</span><span>,</span><span class='string'>"53"</span><span>,</span><span class='string'>"54"</span><span>,</span><span class='string'>"55"</span><span>,</span><span class='string'>"56"</span><span>,</span><span class='string'>"57"</span><span>,</span><span class='string'>"58"</span><span>,</span><span class='string'>"59"</span><span>,</span><span class='string'>"5A"</span><span>,</span><span class='string'>"5B"</span><span>,</span><span class='string'>"5C"</span><span>,</span><span class='string'>"5D"</span><span>,</span><span class='string'>"5E"</span><span>,</span><span class='string'>"5F"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"60"</span><span>,</span><span class='string'>"61"</span><span>,</span><span class='string'>"62"</span><span>,</span><span class='string'>"63"</span><span>,</span><span class='string'>"64"</span><span>,</span><span class='string'>"65"</span><span>,</span><span class='string'>"66"</span><span>,</span><span class='string'>"67"</span><span>,</span><span class='string'>"68"</span><span>,</span><span class='string'>"69"</span><span>,</span><span class='string'>"6A"</span><span>,</span><span class='string'>"6B"</span><span>,</span><span class='string'>"6C"</span><span>,</span><span class='string'>"6D"</span><span>,</span><span class='string'>"6E"</span><span>,</span><span class='string'>"6F"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"70"</span><span>,</span><span class='string'>"71"</span><span>,</span><span class='string'>"72"</span><span>,</span><span class='string'>"73"</span><span>,</span><span class='string'>"74"</span><span>,</span><span class='string'>"75"</span><span>,</span><span class='string'>"76"</span><span>,</span><span class='string'>"77"</span><span>,</span><span class='string'>"78"</span><span>,</span><span class='string'>"79"</span><span>,</span><span class='string'>"7A"</span><span>,</span><span class='string'>"7B"</span><span>,</span><span class='string'>"7C"</span><span>,</span><span class='string'>"7D"</span><span>,</span><span class='string'>"7E"</span><span>,</span><span class='string'>"7F"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"80"</span><span>,</span><span class='string'>"81"</span><span>,</span><span class='string'>"82"</span><span>,</span><span class='string'>"83"</span><span>,</span><span class='string'>"84"</span><span>,</span><span class='string'>"85"</span><span>,</span><span class='string'>"86"</span><span>,</span><span class='string'>"87"</span><span>,</span><span class='string'>"88"</span><span>,</span><span class='string'>"89"</span><span>,</span><span class='string'>"8A"</span><span>,</span><span class='string'>"8B"</span><span>,</span><span class='string'>"8C"</span><span>,</span><span class='string'>"8D"</span><span>,</span><span class='string'>"8E"</span><span>,</span><span class='string'>"8F"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"90"</span><span>,</span><span class='string'>"91"</span><span>,</span><span class='string'>"92"</span><span>,</span><span class='string'>"93"</span><span>,</span><span class='string'>"94"</span><span>,</span><span class='string'>"95"</span><span>,</span><span class='string'>"96"</span><span>,</span><span class='string'>"97"</span><span>,</span><span class='string'>"98"</span><span>,</span><span class='string'>"99"</span><span>,</span><span class='string'>"9A"</span><span>,</span><span class='string'>"9B"</span><span>,</span><span class='string'>"9C"</span><span>,</span><span class='string'>"9D"</span><span>,</span><span class='string'>"9E"</span><span>,</span><span class='string'>"9F"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"A0"</span><span>,</span><span class='string'>"A1"</span><span>,</span><span class='string'>"A2"</span><span>,</span><span class='string'>"A3"</span><span>,</span><span class='string'>"A4"</span><span>,</span><span class='string'>"A5"</span><span>,</span><span class='string'>"A6"</span><span>,</span><span class='string'>"A7"</span><span>,</span><span class='string'>"A8"</span><span>,</span><span class='string'>"A9"</span><span>,</span><span class='string'>"AA"</span><span>,</span><span class='string'>"AB"</span><span>,</span><span class='string'>"AC"</span><span>,</span><span class='string'>"AD"</span><span>,</span><span class='string'>"AE"</span><span>,</span><span class='string'>"AF"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"B0"</span><span>,</span><span class='string'>"B1"</span><span>,</span><span class='string'>"B2"</span><span>,</span><span class='string'>"B3"</span><span>,</span><span class='string'>"B4"</span><span>,</span><span class='string'>"B5"</span><span>,</span><span class='string'>"B6"</span><span>,</span><span class='string'>"B7"</span><span>,</span><span class='string'>"B8"</span><span>,</span><span class='string'>"B9"</span><span>,</span><span class='string'>"BA"</span><span>,</span><span class='string'>"BB"</span><span>,</span><span class='string'>"BC"</span><span>,</span><span class='string'>"BD"</span><span>,</span><span class='string'>"BE"</span><span>,</span><span class='string'>"BF"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"C0"</span><span>,</span><span class='string'>"C1"</span><span>,</span><span class='string'>"C2"</span><span>,</span><span class='string'>"C3"</span><span>,</span><span class='string'>"C4"</span><span>,</span><span class='string'>"C5"</span><span>,</span><span class='string'>"C6"</span><span>,</span><span class='string'>"C7"</span><span>,</span><span class='string'>"C8"</span><span>,</span><span class='string'>"C9"</span><span>,</span><span class='string'>"CA"</span><span>,</span><span class='string'>"CB"</span><span>,</span><span class='string'>"CC"</span><span>,</span><span class='string'>"CD"</span><span>,</span><span class='string'>"CE"</span><span>,</span><span class='string'>"CF"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"D0"</span><span>,</span><span class='string'>"D1"</span><span>,</span><span class='string'>"D2"</span><span>,</span><span class='string'>"D3"</span><span>,</span><span class='string'>"D4"</span><span>,</span><span class='string'>"D5"</span><span>,</span><span class='string'>"D6"</span><span>,</span><span class='string'>"D7"</span><span>,</span><span class='string'>"D8"</span><span>,</span><span class='string'>"D9"</span><span>,</span><span class='string'>"DA"</span><span>,</span><span class='string'>"DB"</span><span>,</span><span class='string'>"DC"</span><span>,</span><span class='string'>"DD"</span><span>,</span><span class='string'>"DE"</span><span>,</span><span class='string'>"DF"</span><span>,       </span></li>
    <li class=''><span>        </span><span class='string'>"E0"</span><span>,</span><span class='string'>"E1"</span><span>,</span><span class='string'>"E2"</span><span>,</span><span class='string'>"E3"</span><span>,</span><span class='string'>"E4"</span><span>,</span><span class='string'>"E5"</span><span>,</span><span class='string'>"E6"</span><span>,</span><span class='string'>"E7"</span><span>,</span><span class='string'>"E8"</span><span>,</span><span class='string'>"E9"</span><span>,</span><span class='string'>"EA"</span><span>,</span><span class='string'>"EB"</span><span>,</span><span class='string'>"EC"</span><span>,</span><span class='string'>"ED"</span><span>,</span><span class='string'>"EE"</span><span>,</span><span class='string'>"EF"</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='string'>"F0"</span><span>,</span><span class='string'>"F1"</span><span>,</span><span class='string'>"F2"</span><span>,</span><span class='string'>"F3"</span><span>,</span><span class='string'>"F4"</span><span>,</span><span class='string'>"F5"</span><span>,</span><span class='string'>"F6"</span><span>,</span><span class='string'>"F7"</span><span>,</span><span class='string'>"F8"</span><span>,</span><span class='string'>"F9"</span><span>,</span><span class='string'>"FA"</span><span>,</span><span class='string'>"FB"</span><span>,</span><span class='string'>"FC"</span><span>,</span><span class='string'>"FD"</span><span>,</span><span class='string'>"FE"</span><span>,</span><span class='string'>"FF"</span><span>      </span></li>
    <li class=''><span>    };       </span></li>
    <li class='alt'><span>    </span><span class='keyword'>private</span><span> </span><span class='keyword'>final</span><span> </span><span class='keyword'>static</span><span> </span><span class='keyword'>byte</span><span>[] val = {       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x00</span><span>,</span><span class='number'>0x01</span><span>,</span><span class='number'>0x02</span><span>,</span><span class='number'>0x03</span><span>,</span><span class='number'>0x04</span><span>,</span><span class='number'>0x05</span><span>,</span><span class='number'>0x06</span><span>,</span><span class='number'>0x07</span><span>,</span><span class='number'>0x08</span><span>,</span><span class='number'>0x09</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x0A</span><span>,</span><span class='number'>0x0B</span><span>,</span><span class='number'>0x0C</span><span>,</span><span class='number'>0x0D</span><span>,</span><span class='number'>0x0E</span><span>,</span><span class='number'>0x0F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x0A</span><span>,</span><span class='number'>0x0B</span><span>,</span><span class='number'>0x0C</span><span>,</span><span class='number'>0x0D</span><span>,</span><span class='number'>0x0E</span><span>,</span><span class='number'>0x0F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class=''><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,       </span></li>
    <li class='alt'><span>        </span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>,</span><span class='number'>0x3F</span><span>      </span></li>
    <li class=''><span>    };       </span></li>
    <li class='alt'><span>    </span><span class='keyword'>public</span><span> </span><span class='keyword'>static</span><span> String escape(String s) {       </span></li>
    <li class=''><span>        StringBuffer sbuf = </span><span class='keyword'>new</span><span> StringBuffer();       </span></li>
    <li class='alt'><span>        </span><span class='keyword'>int</span><span> len = s.length();       </span></li>
    <li class=''><span>        </span><span class='keyword'>for</span><span> (</span><span class='keyword'>int</span><span> i = </span><span class='number'>0</span><span>; i &lt; len; i++) {       </span></li>
    <li class='alt'><span>            </span><span class='keyword'>int</span><span> ch = s.charAt(i);       </span></li>
    <li class=''><span>            </span><span class='keyword'>if</span><span> (ch == ' ') {                        </span><span class='comment'>// space : map to '+'      </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append('+');       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('A' &lt;= ch &amp;&amp; ch &lt;= 'Z') {    </span><span class='comment'>// 'A'..'Z' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('a' &lt;= ch &amp;&amp; ch &lt;= 'z') {    </span><span class='comment'>// 'a'..'z' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('</span><span class='number'>0</span><span>' &lt;= ch &amp;&amp; ch &lt;= '&lt;/span&gt;&lt;span </span><span class='keyword'>class</span><span>=</span><span class='string'>"number"</span><span>&gt;</span><span class='number'>9</span><span>&lt;/span&gt;&lt;span&gt;') {    </span><span class='comment'>// '0'..'9' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> (ch == '-' || ch == '_'       </span><span class='comment'>// unreserved : as it was     </span><span>  </span></li>
    <li class='alt'><span>                || ch == </span><span class='string'>'.'</span><span> || ch == '!'       </span></li>
    <li class=''><span>                || ch == '~' || ch == '*'       </span></li>
    <li class='alt'><span>                || ch == </span><span class='string'>'\''</span><span> || ch == '('       </span></li>
    <li class=''><span>                || ch == ')') {       </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> (ch &lt;= </span><span class='number'>0x007F</span><span>) {              </span><span class='comment'>// other ASCII : map to %XX     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append('%');       </span></li>
    <li class=''><span>                sbuf.append(hex[ch]);       </span></li>
    <li class='alt'><span>            } </span><span class='keyword'>else</span><span> {                                </span><span class='comment'>// unicode : map to %uXXXX     </span><span>  </span></li>
    <li class=''><span>                sbuf.append('%');       </span></li>
    <li class='alt'><span>                sbuf.append('u');       </span></li>
    <li class=''><span>                sbuf.append(hex[(ch &gt;&gt;&gt; </span><span class='number'>8</span><span>)]);       </span></li>
    <li class='alt'><span>                sbuf.append(hex[(</span><span class='number'>0x00FF</span><span> &amp; ch)]);       </span></li>
    <li class=''><span>            }       </span></li>
    <li class='alt'><span>        }       </span></li>
    <li class=''><span>        </span><span class='keyword'>return</span><span> sbuf.toString();       </span></li>
    <li class='alt'><span>    }       </span></li>
    <li class=''><span>    </span><span class='keyword'>public</span><span> </span><span class='keyword'>static</span><span> String unescape(String s) {       </span></li>
    <li class='alt'><span>        StringBuffer sbuf = </span><span class='keyword'>new</span><span> StringBuffer();       </span></li>
    <li class=''><span>        </span><span class='keyword'>int</span><span> i = </span><span class='number'>0</span><span>;       </span></li>
    <li class='alt'><span>        </span><span class='keyword'>int</span><span> len = s.length();       </span></li>
    <li class=''><span>        </span><span class='keyword'>while</span><span> (i &lt; len) {       </span></li>
    <li class='alt'><span>            </span><span class='keyword'>int</span><span> ch = s.charAt(i);       </span></li>
    <li class=''><span>            </span><span class='keyword'>if</span><span> (ch == '+') {                        </span><span class='comment'>// + : map to ' '      </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append(' ');       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('A' &lt;= ch &amp;&amp; ch &lt;= 'Z') {    </span><span class='comment'>// 'A'..'Z' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('a' &lt;= ch &amp;&amp; ch &lt;= 'z') {    </span><span class='comment'>// 'a'..'z' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> ('</span><span class='number'>0</span><span>' &lt;= ch &amp;&amp; ch &lt;= '&lt;/span&gt;&lt;span </span><span class='keyword'>class</span><span>=</span><span class='string'>"number"</span><span>&gt;</span><span class='number'>9</span><span>&lt;/span&gt;&lt;span&gt;') {    </span><span class='comment'>// '0'..'9' : as it was     </span><span>  </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> (ch == '-' || ch == '_'       </span><span class='comment'>// unreserved : as it was     </span><span>  </span></li>
    <li class='alt'><span>                || ch == </span><span class='string'>'.'</span><span> || ch == '!'       </span></li>
    <li class=''><span>                || ch == '~' || ch == '*'       </span></li>
    <li class='alt'><span>                || ch == </span><span class='string'>'\''</span><span> || ch == '('       </span></li>
    <li class=''><span>                || ch == ')') {       </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)ch);       </span></li>
    <li class=''><span>            } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span> (ch == '%') {       </span></li>
    <li class='alt'><span>                </span><span class='keyword'>int</span><span> cint = </span><span class='number'>0</span><span>;       </span></li>
    <li class=''><span>                </span><span class='keyword'>if</span><span> ('u' != s.charAt(i+</span><span class='number'>1</span><span>)) {         </span><span class='comment'>// %XX : map to ascii(XX)     </span><span>  </span></li>
    <li class='alt'><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>1</span><span>)];       </span></li>
    <li class=''><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>2</span><span>)];       </span></li>
    <li class='alt'><span>                    i+=</span><span class='number'>2</span><span>;       </span></li>
    <li class=''><span>                } </span><span class='keyword'>else</span><span> {                            </span><span class='comment'>// %uXXXX : map to unicode(XXXX)     </span><span>  </span></li>
    <li class='alt'><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>2</span><span>)];       </span></li>
    <li class=''><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>3</span><span>)];       </span></li>
    <li class='alt'><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>4</span><span>)];       </span></li>
    <li class=''><span>                    cint = (cint &lt;&lt; </span><span class='number'>4</span><span>) | val[s.charAt(i+</span><span class='number'>5</span><span>)];       </span></li>
    <li class='alt'><span>                    i+=</span><span class='number'>5</span><span>;       </span></li>
    <li class=''><span>                }       </span></li>
    <li class='alt'><span>                sbuf.append((</span><span class='keyword'>char</span><span>)cint);       </span></li>
    <li class=''><span>            }       </span></li>
    <li class='alt'><span>            i++;       </span></li>
    <li class=''><span>        }       </span></li>
    <li class='alt'><span>        </span><span class='keyword'>return</span><span> sbuf.toString();       </span></li>
    <li class=''><span>    }       </span></li>
    <li class='alt'><span/><span class='comment'>//    public static void main(String[] args) {     </span><span>  </span></li>
    <li class=''><span/><span class='comment'>//        String stest = "中文1234 abcd[]()&lt;+&gt;,.~\\";     </span><span>  </span></li>
    <li class='alt'><span/><span class='comment'>//        System.out.println(stest);     </span><span>  </span></li>
    <li class=''><span/><span class='comment'>//        System.out.println(escape(stest));     </span><span>  </span></li>
    <li class='alt'><span/><span class='comment'>//        System.out.println(unescape(escape(stest)));     </span><span>  </span></li>
    <li class=''><span/><span class='comment'>//    }     </span><span>  </span></li>
    <li class='alt'><span>}       </span></li>
</ol>
</div>

相关推荐

    Ajax中文乱码问题解决方案

    在IT行业中,Ajax(Asynchronous JavaScript ...通过理解和应用这些解决方案,开发者可以有效地避免和解决Ajax请求中的中文乱码问题,从而提高用户体验。记住,良好的编码习惯和对字符编码的理解是解决此类问题的关键。

    c# ajax post数据乱码解决

    在开发过程中,尤其是在使用C#与ASP.NET进行Web开发时,经常会遇到客户端通过Ajax向服务器端发送Post请求后,服务器端接收到的数据出现乱码的问题。这种情况不仅影响用户体验,也可能会导致程序逻辑出现问题。本文将...

    Ajax中文乱码问题解决方法

    总的来说,解决Ajax中文乱码问题的关键在于明确和统一字符编码。通过设置请求头、响应头、统一前后端编码以及必要时的数据转换,我们可以有效地避免和修复这个问题。在实际开发过程中,良好的编码规范和测试实践也是...

    Ajax 中文乱码问题

    本文将详细探讨如何解决Ajax请求过程中的中文乱码问题,并提供具体的解决方案。 #### 二、问题分析 在使用Ajax进行数据传输时,中文乱码问题通常出现在以下两个环节: 1. **发送表单数据时中文乱码**:当通过Ajax...

    ajax提交中文乱码解决方法

    综上所述,解决AJAX提交中文乱码的问题主要涉及客户端和服务端两个方面的编码处理。在客户端通过`URLEncoder`进行编码,并确保Content-Type正确设置;在服务端则通过`URLDecoder`进行解码,并对Servlet容器及程序中...

    ajax提交,后台中文乱码解决

    在IT领域,尤其是在Web开发中,处理中文字符时经常遇到的一个问题是中文乱码。这个问题尤其在使用Ajax进行异步数据提交时更为突出。Ajax(Asynchronous JavaScript and XML)是一种在无需重新加载整个网页的情况下,...

    ajax中文乱码如何解决

    在本文中,我们将深入探讨如何解决使用Ajax进行POST请求时出现的中文乱码问题。首先,我们要理解Ajax中文乱码的根源,它通常涉及到字符编码的不一致,尤其是在客户端与服务器之间的通信过程中。 Ajax(Asynchronous...

    Struts框架下Ajax发送中文乱码问题的解决

    在实际应用中,我们常常会遇到Ajax与服务器交互时出现中文乱码的问题,尤其是在发送和接收中文数据时。这个问题主要涉及到字符编码的处理,解决起来需要对HTTP协议、字符编码原理以及Struts框架的工作机制有一定的...

    Ajax使用POST提交中文乱码问题.docx

    在使用Ajax进行POST提交时,中文字符可能会遇到乱码问题,这是因为数据在传输过程中编码与解码不匹配导致的。Ajax以UTF-8编码发送数据,但服务器端可能使用了不同的字符集(如GB2312)来解析,这就产生了乱码。本文...

    ajax到servlet乱码解决

    通过使用 `encodeURI()` 函数在 AJAX 客户端编码 URL 参数和使用 `URLDecoder` 类在 Servlet 服务器端解码 URL 参数,我们可以解决 AJAX 到 Servlet 乱码问题。这样,我们的应用程序可以正确地处理中文参数,从而...

    解决ajax乱码和页面传值问题

    Ajax发送和接收数据时,乱码问题主要出现在两个环节:发送请求时的数据编码和服务器返回数据的解码。以下是解决乱码的关键步骤: 1. 数据编码:在发送Ajax请求前,确保你的数据已经正确编码。如果数据包含中文或...

    ajax乱码解决方案

    总的来说,解决Ajax乱码问题的关键在于理解字符编码的工作原理,确保数据在整个传输过程中的编码与解码一致性,以及正确设置服务器和客户端的编码设置。在实际应用中,还应考虑跨平台和跨浏览器的兼容性,以确保解决...

    AJAX在Post中文的时候解决乱码的方法

    ### AJAX在POST中文时解决乱码的方法 #### 一、问题背景 在使用AJAX进行数据请求时,尤其是在处理中文字符的过程中,经常会遇到一个常见问题:即通过AJAX请求得到的`responseText`中的中文字符出现乱码。这个问题...

    ajax解决乱码问题

    在IT领域,尤其是在Web开发中,处理中文或其他非英文字符集时,经常遇到的一个问题是乱码。这主要是因为客户端和服务器端对字符编码的理解不一致所导致的。本文将深入探讨如何利用AJAX(Asynchronous JavaScript and...

    ajax get请求中文参数乱码解决

    在进行Web开发时,我们经常会遇到Ajax GET请求发送中文参数时出现乱码的问题。这个问题主要源于HTTP协议在处理非ASCII字符时的编码不一致。在本文中,我们将深入探讨这个问题,以及如何通过不同的方法来解决它。 ...

    escape --AJAX和JS传中文乱码的最终解决方案

    在开发Web应用时,我们经常会遇到一个棘手的问题:AJAX和JavaScript在处理中文字符时出现乱码。这是因为浏览器、服务器以及数据编码之间的不匹配所导致的。"escape"函数在JavaScript中扮演着重要的角色,它可以帮助...

    AJAX在Post中文的时候乱码的解决方法

    AJAX(Asynchronous JavaScript and XML)是一种在Web开发中用于实现异步数据交换的技术,但是在使用AJAX进行POST操作时,遇到中文乱码的问题是一种常见的现象。这种问题的出现是由于AJAX在处理服务器返回的响应时,...

    extjs 前后台交互参数出现中文乱码问题的解决方法

    通过上述方法,我们可以有效地解决在使用ExtJS进行Ajax同步请求时,中文参数出现乱码的问题。关键是保证前后端编码的一致性,以及正确地使用编码与解码函数。此外,还可以考虑在服务器端配置文件中设置统一的字符集...

    ajax POST 与GET提交的区别

    - **POST**: 同样存在乱码问题,可以通过设置Content-Type和服务器端编码来解决。 7. **实现细节**: - **GET**: 参数直接拼接在URL后面,如 `http://example.com?name=value`。 - **POST**: 需要通过设置Content...

Global site tag (gtag.js) - Google Analytics