SpringMVC returns Ajax data in response

  1. Prepare jquery.min.js file and store it in js file under webapp
  2. js fileSince the configured front controller intercepts all requests, it is necessary to let the front controller release the static resources, so add in spring-mvc.xml:
 <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

Unblock3. If you need to transfer the data of the page to the server and encapsulate it in the manner of javabean, as a parameter, you need to import dependencies: rely4. At this point, you can fill in the code in html. First import the imported jquery.min.js file, and Import jquerysecondly edit the code, for example:

$(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);
                }

            })
        });
    });

Examples5. Complete the background service, you canBackstage

Published 12 original articles · Likes0 · Visits 147

Guess you like

Origin blog.csdn.net/weixin_44065691/article/details/105068098