历经辛苦,终于在JQuery中实现了Ajax Json的使用,进行记录:
1. 首先在html页面上加入
<script type="text/javascript" src="../resources/json2.js"></script>
<script type="text/javascript" src="../resources/jquery-1.11.3.js"></script>
<script type="text/javascript" src="../resources/jquery.json-2.5.1.js"></script>
2. $.ajax({
type : "POST",
dataType : "json",
contentType : "application/json;charset=utf-8",
url : "/oneparty/user/AddUser.do",
data:JSON.stringify(newuser),
success:function(data){ //recive Json data from server.
/*user = JSON.parse(data);
//这句开始时一直在测试,但后来发现不行,因为
//返回值is already parsed, you don't need to parse it again.
//If you parse it again it will perform a toString-cast first so you're parsing
arg0 = data.userName;
arg1 = data.password;
arg2 = data.department;
arg3 = data.headship;
arg4 = data.sex;
arg5 = data.old;
alert(arg5);
//将JSON对象的内容添加到表格中
updateTable(arg0,arg1,arg2,arg3,arg4,arg5);
//}
},
error : function() {
alert("error")
}
})
3. 服务器端返回中文乱码的解决方式
spring-mvc.xml
<span style="font-size:18px;"><!-- springmvc传json值时的乱码解决 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven></span>