遇到问题---getJson传给后台的参数乱码

前端之家收集整理的这篇文章主要介绍了遇到问题---getJson传给后台的参数乱码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


项目中使用spring sturts action 传值 出现乱码 而且google浏览器中没乱码 ie下传值乱码


在jsp中使用getJson传值

$.getJSON("<%=basePath%>/query/searchCompareTable?geneName="+geneName+"&fieldName="+fieldName+"&minValue="+minValue+"&maxValue="+maxValue,function(datatable) {


其中geneName 和 fieldName是 中文或者英文名



java中 action接收为set接收

 private String geneName;
	private String fieldName;
	private double minValue;
	private double maxValue;
	public void setGeneName(String geneName) {
		this.geneName = geneName;
	}
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
public void setMinValue(double minValue) {
		this.minValue = minValue;
	}
public void setMaxValue(double maxValue) {
		this.maxValue = maxValue;
	}



这个时候服务器的tomcat的server.xml配置 已经设置成 utf-8




jsp页面头部为

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<Meta charset="UTF-8">
<title>gseInfo</title>
<link type="text/css" rel="stylesheet" href="css/skin.css" />
<link type="text/css" rel="stylesheet" href="css/genesBoxUI.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script src="js/controls.js"></script>
<script>
	document.createElement('header');
	document.createElement('nav');
	document.createElement('aside');
	document.createElement('section');
</script>


这个时候发现 中文参数 乱码




解法一

解决办法

jsp中 getJson 中英文参数加上一次转码

encodeURI(geneName)

$.getJSON("<%=basePath%>/query/searchCompareTable?geneName="+encodeURI(geneName)+"&fieldName="+encodeURI(fieldName)+"&minValue="+minValue+"&maxValue="+maxValue,function(datatable) {

action中不解码





解法二


解决办法

jsp中 getJson 中英文参数加上两次转码

encodeURI(encodeURI(geneName))

encodeURI(encodeURI(fieldName))

如下:

$.getJSON("<%=basePath%>/query/searchCompareTable?geneName="+encodeURI(encodeURI(geneName))+"&fieldName="+encodeURI(encodeURI(fieldName))+"&minValue="+minValue+"&maxValue="+maxValue,function(datatable) {


action中解码

	compareResultHtml="";
		try {
			geneName=URLDecoder.decode(geneName,"utf-8");
			fieldName=URLDecoder.decode(fieldName,"utf-8");
		} catch (UnsupportedEncodingException e1) {			
			e1.printStackTrace();
		}
原文链接:https://www.f2er.com/json/289921.html

猜你在找的Json相关文章