Maison  >  Article  >  interface Web  >  JavaScript乱码

JavaScript乱码

高洛峰
高洛峰original
2016-11-26 14:48:241271parcourir

javascript 中对于参数编码:


JavaScript代码  编码一次
1.url=encodeURI(url);  //注意:encodeURI()的参数是整个URL,包含请求地址和参数


服务器端获取参数后转码:


Java代码  转码一次
1.String linename = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");  


或:

JavaScript代码  编码两次
1.url=encodeURI(encodeURI(url)); //用了2次encodeURI  


服务器端获取:


Java代码  解码一次
1.String linename = request.getParameter(name); 
2.//java  : 字符解码 
3.linename = java.net.URLDecoder.decode(linename , "UTF-8"); 


PS:前后台进行数据交换时,最好使用JSONObject对数据进行封装。

 

测试
服务器:WebLogic10.x
浏览器:IE9.0
测试一:
前台JavaScript
//这里的Ext.util.JSON.encode为的是将一个JSONObject转为一个JSON字符串,与encodeURI的含义不同
//caseEndReport中的fileUpload设置为ture
var caseEndReportTab = Ext.util.JSON.encode(caseEndReport.getForm().getValues(false));
caseEndReport.getForm().submit({
     url:'payCaseManage.do?oper=saveCaseEndReport&caseEndReportTab='+caseEndReportTab,
                         success: function(form, action){
                                          Ext.Msg.alert('提示', "信息保存成功!");
                         },
                         failure: function(){
                                          Ext.Msg.alert('提示', '信息保存失败,您没有操作权限!');
                         }
});
后台Java代码
String caseEndReportTab = request.getParameter("caseEndReportTab");        
JSONObject caseEndReportJson = JSONObject.fromObject(caseEndReportTab);//将JSON字符串转为JSON对象
System.out.println(accreditationJson.toString());


如果在Controller里面加入
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
则上面的代码输出无乱码。


如果在Controller里面加入
response.setCharacterEncoding("UTF-8");//不加入request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
上面的代码输出中文乱码,需要将
JSONObject caseEndReportJson = JSONObject.fromObject(caseEndReportTab);
改为
JSONObject caseEndReportJson = JSONObject.fromObject(new String(caseEndReportTab.getBytes("ISO8859_1"),"UTF-8"));
才输出正确。


测试二:
前台JavaScript
// queryForm 中的fileUpload设置为false,query为JSON字符串,且含有中文
//Ext.util.JSON.encode只是将一个JSONObject转为一个JSON字符串,与encodeURI的含义不同
var query = Ext.util.JSON.encode(queryForm.getForm().getValues(false));
caseStore.proxy = new Ext.data.HttpProxy({url:"payCaseManage.do?oper=listCase&query="+query});


后台Java代码
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
String query = request.getParameter("query");
//或者String query = new String(request.getParameter("query").getBytes("ISO8859_1"),"UTF-8");
JSONObject queryObject = JSONObject.fromObject(query);
String county = queryObject.getString("county");
String city = queryObject.getString("city");
System.out.println(city+county);
均输出中文乱码


测试三:
前台JavaScript
//前台对url两次encodeURI, query字符串中包含中文内容
var query = Ext.util.JSON.encode(queryForm.getForm().getValues(false));
var url = "payCaseManage.do?oper=listCase&query="+query;
var dataUrl = encodeURI(encodeURI(url));
caseStore.proxy = new Ext.data.HttpProxy({url:dataUrl});
//后台Java对request.getParameter(" query ")进行java.net.URLDecoder.decode 一次
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
String query = java.net.URLDecoder.decode(request.getParameter("query"),"UTF-8");
JSONObject queryObject = JSONObject.fromObject(query);
String county = queryObject.getString("county");
String city = queryObject.getString("city");
System.out.println(city+county);
结果输出中文正确


测试四:
前台JavaScript
//对url两次encodeURI,caseRootId中包含中文内容
caseTreeLoaderDataUrl = 'payCaseManage.do?oper=treeLoad&nodeId=' + node.id+'&text='+node.text+'&caseRootId='+caseRootId;
caseTree.loader.dataUrl = encodeURI(encodeURI(caseTreeLoaderDataUrl));
后台Java
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
//对request.getParameter("caseRootId")进行java.net.URLDecoder.decode 一次
String caseRootId = java.net.URLDecoder.decode(request.getParameter("caseRootId"),"UTF-8");
System.out.println(caseRootId);
结果输出中文正确


测试五:
前台JavaScript
//不对url进行encodeURI处理。city和county均包含中文内容
var city = queryForm.getForm().findField("city").getValue();
var county = queryForm.getForm().findField("county").getValue();
caseStore.proxy = new Ext.data.HttpProxy({url:"payCaseManage.do?oper=listCase&city="+city+"&county="+county"});
后台Jave代码
//不使用java.net.URLDecoder.decode对request.getParameter()进行处理
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
String county = request.getParameter("county");
String city = request.getParameter("city");
System.out.println(city+county);
输出中文乱码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn