http,javascript的编码解码

http,javascript的编码解码

请求与响应的编码应分开分析
两者的编码,解码处理是相对独立的流程
依赖于相对独立的header: request header, response header

ajax相关
http的请求分三部分
header iso-8859-1
uri  utf-8编码(待实验)
body utf-8编码(待实验)

响应
header  iso-8859-1
body  由服务器指定编码方式
响应的数据由浏览器根据返回的头部的编码方式解码
解码后传递给 javascript处理
最好在响应头部指定具体的编码方式


eg

  1. </pre><p><pre name="code" class="html"><!doctype html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="GBK">  
  5.     <script src="jquery.js"></script>  
  6.     <script src="app.js"></script>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.     </div>  
  11. </body>  
  12. </html>  


app.js
访问 b.jsp butf8.jsp均正常显示中文

[javascript] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $(function(){  
  2.     $.ajax({  
  3.         "url": "b.jsp", // "url": "butf8.jsp",  
  4.         "type": "get",  
  5.         "dataType": "json",  
  6.         "success": function(data){  
  7.             alert(data["yy"]);  
  8.             $("div").text(data["yy"]);  
  9.         }  
  10.           
  11.     });  
  12. });  




b.jsp
保存为gbk文件

  1. <%@ page contentType="text/json;charset=gbk" %>  
  2.   
  3. <%  
  4.     out.println("{\"中文\":\"xx6\",\"yy\":\"英文6\"}");  
  5. %>  



butf8.jsp
保存为utf8文件

    1. <%@ page contentType="text/json;charset=utf-8" %>  
    2. <%  
    3.     out.println("{\"中文\":\"xx5\",\"yy\":\"英文5\"}");  

%>  

 

======================================

app.js

[javascript] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $(function(){  
  2.     $("#ss").on("click", function(){  
  3.         var p = $("p").text();  
  4.         alert(p);  
  5.         var pp = {"a": p+p, "b":"1"};  
  6.         alert(JSON.stringify(pp));  
  7.           
  8.         $.ajax({  
  9.         "url": "b.jsp",  
  10.         "contentType":"text/json",  
  11.         "data": JSON.stringify(pp),  
  12.         //"data":pp,  
  13.         "type": "post",  
  14.         "dataType": "json",  
  15.         "success": function(data){  
  16.             //alert(data["yy"]);  
  17.             $("div").text(data["yy"]);  
  18.             $("p").text(data["yy"]);  
  19.             }  
  20.         });  
  21.           
  22.     });  
  23. });  

b.jsp

  1. <%@ page import="java.io.InputStream" %>  
  2. <%@ page contentType="text/json;charset=gbk" %>  
  3.   
  4. <%  
  5.     out.println("{\"中文\":\"xx6\",\"yy\":\"英文6\"}");  
  6. %>  
  7.   
  8. <%  
  9.         byte[] bytes = new byte[1024 * 1024];    
  10.                 InputStream is = request.getInputStream();    
  11.         
  12.                 int nRead = 1;    
  13.                 int nTotalRead = 0;    
  14.                 while (nRead > 0) {    
  15.                     nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);    
  16.                     if (nRead > 0)    
  17.                         nTotalRead = nTotalRead + nRead;    
  18.                 }    
  19.                 String str = new String(bytes, 0, nTotalRead, "gbk");    
  20.                 System.out.println("Str:" + str);    
  21. %>  


原文为 "英文6英文6"

输出为 Str:{"a":"鑻辨枃6鑻辨枃6","b":"1"}

  1. String str = new String(bytes, 0, nTotalRead, "utf-8");    

输出为 Str:{"a":"英文6英文6","b":"1"}

说明: javascript提交时("contentType":"text/json",),("contentType":"application/json"), 直接把 英文6英文6 编码为 utf-8 字节流

post观察的内容为:  {"a":"鑻辨枃6鑻辨枃6","b":"1"}

按gbk解码为 "鑻辨枃6鑻辨枃6"   , 按utf-8解码为   英文6英文6

********************

post缺省提交时
a=%E8%8B%B1%E6%96%876%E8%8B%B1%E6%96%876&b=1

utf-8 uriencode

 

 

posted @ 2016-06-01 09:08  zhao1949  阅读(333)  评论(0编辑  收藏  举报