- 准备jquery.min.js文件,存储至webapp下js文件中
- 由于配置的前端控制器,拦截了所有的请求,所以需要让前端控制器,释放静态资源于是,在spring-mvc.xml中添加:
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
3.如果需要将页面的数据传送到服务器以javabean的方式封装好,作为参数,则需要导依赖:4.至此,可以在html中填写代码了。首先引入导入的jquery.min.js文件,其次进行代码编辑,举例:
$(function () {
$("#btn").click(function (){
$.ajax({
//编写json格式数据
url: "controller3/testAjax",
contentType: "application/json;charset=UTF-8",
data:'{"uname":"huang","age":30}',
dataType: "json",
type: "post",
success: function (data) {
//data服务器端响应的json数据,进行解析
alert(data);
alert(data.uname);
alert(data.age);
}
})
});
});
5.完成后台服务,即可