javascript 中對於參數編碼:
JavaScript代碼 編碼一次
1.url=encodeURI(url); //注意:encodeURI()的參數是整個URL,包含請求位址和參數
伺服器端取得參數後轉碼:
Java代碼 轉碼一次
1.String linename = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
或:
1.url=encodeURI(encodeURI(url)); //用了2次encodeURI
伺服器端取得:
String Java 。好使用JSONObject對資料進行封裝。
測試
伺服器:WebLogic10.x
測試一:
前台JavaScript//這裡的Ext.util.JSON.encode為的是將一個JSONSON與encodeURI的意義不同
//caseEndReport中的fileUpload設定為ture
var caseEndReportTab = Ext.util.JSON.encode(caseEndReport.getForm().getValues(false));
caseEndReport.getForm().submit(
));
caseEndReport.getForm()。 url:'payCaseManage.do?oper=saveCaseEndReport&caseEndReportTab='+caseEndReportTab,
success: function(form, action){
Ext.Msg.alert('提示', "信息保存成功!");
},
failure: function(){
}
});
後台Java程式碼
String caseEndReportTab = request.getParameter(abase ;
JSONObject caseEndReportJson = JSONObject.fromObject(caseEndReportTab);//將JSON字串轉為JSON物件
System.out.println(accreditationJson.toString());mcome; UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
則上面的程式碼輸出無亂碼。
response.setCharacterEncoding("UTF-8");//不加入request.setCharacterEncoding("UTF-8");
response.setContentType("application/x-json");
response.setContentType("application/x-json");上面的程式碼輸出中文亂碼,需要將
JSONObject caseEndReportJson = JSONObject.fromObject(caseEndReportTab);
改為
JSONObject caseEndReportJson = JSONObject.fromObject(new String(caseEndReportTab.getBytes""ISO885" ;
測試二:
前台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.setlifType(jappson" ");
String query = request.getParameter("query");
//或String query = new String(request.getParameter("query").getBytes("ISO8859_1"),"UTF-8");
JSONObject queryObject = JSONObject.fromObject(query);
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");
res.Contin. application/x-json");
String query = java.net.URLDecoder.decode(request.getParameter("query"),"UTF-8");
JSONObject queryObject = JSONObject.fromObject(query);
String countyObject = queryObject.getString("county");
String city = queryObject.getString("city");
System.out.println(city+county);
結果輸出中文正確
測試四:
前台Java //對url兩次encodeURI,caseRootId中包含中文內容
caseTreeLoaderDataUrl = 'payCaseManage.do?oper=treeLoad&nodeId=' + node.id+'&text='+node.text+'&caseRootId='+caseRootId; dataUrl = encodeURI(encodeURI(caseTreeLoaderDataUrl));
後台Java
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");res。 ;
//對request.getParameter("caseRootId")進行java.net.URLDecoder.decode 一次
String caseRootId = java.net.URLDecoder.decode(request.getParameter("caseRootId"),"UTF-8");
System.out.println(caseRootId);
結果輸出中文正確
測試五:
前台JavaScript
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-jsonString");
response.setContentType("application/x-jsonString");
response.setContentType("application/x-jsonString"); county = request.getParameter("county");
String city = request.getParameter("city");
System.out.println(city+county);
輸出中文亂碼