1、项目中使用jackson所需要的依赖包
jackson需要的maven依赖
<!--Jackson required包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
2、servlet层将数据转化为json格式
3、前台页面使用
<html>
<head>
<title>使用json格式的数据</title><script type="text/javascript">
function dosearch(){
//1,创建异步对象
var xmlHttp = new XMLHttpRequest();
//2,绑定事件
xmlHttp.onreadystatechange = function() {
if( xmlHttp.readystate == 4 && xmlHttp.status == 200){
var data = xmlHttp.responseText;
//eval定执行括亏甲的代码,把json数据转换为json对象
var jsonobj = eval("(” +data + ")");
//更新dom对象
document.getElementById( "proname " ) .value = jsonobj.name;
}
}
//3.初始异步对象的请求参数
var proid = document.getElementById("proid" ).value;
xmlHttp.open("get", "queryjson?proid="+proid,true);
//4.发送请求
xmlHttp. send();
}
</script>
</head>
<body>
<p>ajax请求使用json格式的数据</p>
<table>
<tr>
<td>省份编号:</td>
<td><input type="text" id="proid">
<input type="button" value="搜索"onclick="dosearch()"></td>
</tr>
<tr>
<td>省份名称:</td>
<td><input type="text" id="proname"></td>
</tr>
<tr>
<td>省份简称:</td>
<td><input type="text" id="projiancheng"></td>
</tr>
<tr>
<td>省会名称:</td>
<td><input type="text" id="proshenghui"></td>
</tr>
</table>
</body>
</html>
4、启动运行,打开页面后可以按F12查看响应
这差不多就是简单地使用ajax进行响应的过程了