springMVC响应返回Ajax数据

  1. 准备jquery.min.js文件,存储至webapp下js文件中
  2. 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文件,导入jquery其次进行代码编辑,举例:

$(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.完成后台服务,即可后台

发布了12 篇原创文章 · 获赞 0 · 访问量 147

猜你喜欢

转载自blog.csdn.net/weixin_44065691/article/details/105068098