关于AJAX POST数据编码

        在最近学习AJAX中,通过POST方式发送的数据在服务器端接收的时候通过检测,发现中文编码不能很好的正确读取,全是乱码。怎么解决这个问题,查询了一下,得知如下知识
         Javascript默认编码方式Unicode,这样传递到服务器端的数据不能够正确读取,尽管采用Unicode解码,有些郁闷。采用了几种方式,无功。
        在JAVA环境下解决这个问题,如下所示:
        一。客户端获取的值使用 escape() JS函数进行编码
        二。服务器端调用一个已经写好的函数进行解码操作,这个JAVA解码函数如下:
       
 1 ExpandedBlockStart.gif ContractedBlock.gif      public   static  String  unescape(String src) dot.gif {
 2InBlock.gif        StringBuffer tmp = new StringBuffer();
 3InBlock.gif        tmp.ensureCapacity(src.length());
 4InBlock.gif        int  lastPos=0,pos=0;  char ch;
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        while (lastPos<src.length())dot.gif{
 6InBlock.gif            pos = src.indexOf("%",lastPos);
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            if (pos == lastPos)dot.gif{
 8ExpandedSubBlockStart.gifContractedSubBlock.gif                if (src.charAt(pos+1)=='u')dot.gif{
 9InBlock.gif                    ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16);
10InBlock.gif                    tmp.append(ch);
11InBlock.gif                    lastPos = pos+6;
12ExpandedSubBlockStart.gifContractedSubBlock.gif                }
elsedot.gif{
13InBlock.gif                    ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16);
14InBlock.gif                    tmp.append(ch);
15InBlock.gif                    lastPos = pos+3;
16ExpandedSubBlockEnd.gif                }

17ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
18ExpandedSubBlockStart.gifContractedSubBlock.gif                if (pos == -1)dot.gif{
19InBlock.gif                    tmp.append(src.substring(lastPos));
20InBlock.gif                    lastPos=src.length();
21ExpandedSubBlockStart.gifContractedSubBlock.gif                }
elsedot.gif{
22InBlock.gif                    tmp.append(src.substring(lastPos,pos));
23InBlock.gif                    lastPos=pos;
24ExpandedSubBlockEnd.gif                }
    
25ExpandedSubBlockEnd.gif            }
  
26ExpandedSubBlockEnd.gif        }
  
27InBlock.gif        return tmp.toString(); 
28ExpandedBlockEnd.gif    }
  

        这样,服务器端就可以完好的解码过来。
-。提交数据页面

<!-- 必须指定输出语言格式,这是js获取的字符串的正确显示的关键 -->
<% @ page language = " java "  contentType = " text/html;charset=GBK " %>
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head >
< title > 无标题文档 </ title >
< script language = " javascript "  src = " js\request.js " >     
</ script >
< script language = " javascript " >
function postData(){
    var url 
=   " testpage.jsp?name=yongboy&time= "   +   new  Date().getTime();
    var temp 
=   " 你好,测试中文中 " ;
    
// 下面这一步是必须!
    var sinfo  =  escape( temp );      // 获得输入的值
    
// 提交数据
    post_request( url, sinfo,  " text "  );
}
// AJAX出现结果,处理变化,在 js\request.js 中已经定义
function pageChange( responseText ){
    var div 
=  document.getElementById(  " show "  );
    div.innerHTML 
=  responseText;
}

</ script >
</ head >

< body >
< input type = " submit "  name = " Submit "  value = " 提交 "  onclick = " postData() "   />
< div id = " show " ></ div >
</ body >
</ html >

二。后台处理数据页面
<!-- 注意,下面制定该页面的文字编码是必须的! -->
<% @page language = " java "  contentType = " text/html;charset=GBK " %>  
<!-- 引入命名空间 -->
<% @ page  import = " handle.* "   %>
<%
    
// 清除缓存
    response.setHeader( " Cache-Control " " no-cache " );
    
// 读取从客户端发送过来的数据
    StringBuffer sb  =   new  StringBuffer();
        
    BufferedReader br 
=  request.getReader();    
    String len 
=   null ;
    
if ( ( len  =  br.readLine() )  !=   null  ){
        sb.append( len );
    }
    String body 
=  sb.toString();
    
// 注:下面的步骤是必须的
    body  =  StringUtil.unescape( body );
    
    System.out.println( 
" body1 =  "   +  body );
    
    out.write( body );
%>
       
例子如下所示:
                注:js\request.js 脚本文件那如下:
None.gif      var  http_request  =   false ;
None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
function  init_request() dot.gif {
InBlock.gif        http_request 
= false;
InBlock.gif        
//??????????XMLHttpRequest????
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if(window.XMLHttpRequest) dot.gif//Mozilla ??????
InBlock.gif
            http_request = new XMLHttpRequest();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (http_request.overrideMimeType) dot.gif{//????MiME????
InBlock.gif
                http_request.overrideMimeType("text/xml");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else if (window.ActiveXObject) dot.gif// IE??????
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try dot.gif{
InBlock.gif                    http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (e) dot.gif{}
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (!http_request) dot.gif// ??????????????????????
InBlock.gif
            window.alert("????????XMLHttpRequest????????.");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }
    
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
function  get_request( url ) dot.gif {
InBlock.gif        init_request();
InBlock.gif        http_request.onreadystatechange 
= processRequest;
InBlock.gif        http_request.open(
"GET", url, true);
InBlock.gif        http_request.send(
null);
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
function  get_request( url, type ) dot.gif {
InBlock.gif        init_request();
InBlock.gif        
if( type == "text" )
InBlock.gif                http_request.onreadystatechange 
= processRequest;
InBlock.gif        
else if( type == "xml" )
InBlock.gif            http_request.onreadystatechange 
= processXmlRequest;
InBlock.gif        http_request.open(
"GET", url, true);
InBlock.gif        http_request.send(
null);
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
// the user can custom the function
ExpandedBlockStart.gifContractedBlock.gif
     function  get_request2( url, myProcess ) dot.gif {
InBlock.gif        init_request();        
InBlock.gif        http_request.onreadystatechange 
= myProcess;
InBlock.gif        http_request.open(
"GET", url, true);
InBlock.gif        http_request.send(
null);
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
function  post_request( url, sinfo )  dot.gif {//????????????????????????????????????
InBlock.gif
        
InBlock.gif        init_request();
InBlock.gif        http_request.onreadystatechange 
= processRequest;
InBlock.gif
InBlock.gif        http_request.open(
"POST", url, true);
InBlock.gif        http_request.setRequestHeader(
"Content-Length",sinfo.length);    
InBlock.gif        http_request.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
InBlock.gif        http_request.send(sinfo);
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
function  post_request( url, sinfo, type )  dot.gif {//????????????????????????????????????
InBlock.gif
        
InBlock.gif        init_request();
InBlock.gif        
if( type == "text" )
InBlock.gif                http_request.onreadystatechange 
= processRequest;
InBlock.gif        
else if( type == "xml" )
InBlock.gif                http_request.onreadystatechange 
= processXmlRequest;
InBlock.gif        http_request.open(
"POST", url, true);
InBlock.gif
//        http_request.setRequestHeader("Content-Length",sinfo.length);    
InBlock.gif
        http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
InBlock.gif        http_request.send(sinfo);
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
// this function gave the user too much free to write his method
ExpandedBlockStart.gifContractedBlock.gif
     function  post_request2( url, sinfo, myProcess )  dot.gif {//????????????????????????????????????
InBlock.gif
        
InBlock.gif        init_request();
InBlock.gif        http_request.onreadystatechange 
= myProcess;
InBlock.gif
InBlock.gif        http_request.open(
"POST", url, true);
InBlock.gif        http_request.setRequestHeader(
"Content-Length",sinfo.length);    
InBlock.gif        http_request.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
InBlock.gif        http_request.send(sinfo);
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
//  text
ExpandedBlockStart.gifContractedBlock.gif
     function  processRequest()  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (http_request.readyState == 4dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (http_request.status == 200dot.gif
InBlock.gif               pageChange( http_request.responseText );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif
InBlock.gif                alert(
"connect the server wrong!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif    
None.gif    
// xml
ExpandedBlockStart.gifContractedBlock.gif
     function  processXmlRequest()  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (http_request.readyState == 4dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (http_request.status == 200dot.gif{
InBlock.gif               pageChange( http_request.responseXml );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif{
InBlock.gif                alert(
"connect the server wrong!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
  
        贴出来,为了记忆,也为了他人吧。

转载于:https://www.cnblogs.com/yongboy/archive/2006/03/11/348010.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值