发ajax响应json格式数据

2021-04-14 07:26

阅读:452

标签:content   webapp   coding   ase   ica   compiler   span   creat   lease   

1、maven依赖

xml version="1.0" encoding="UTF-8"?>

project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  modelVersion>4.0.0modelVersion>

  groupId>com.ly.mvcgroupId>
  artifactId>springmvc02artifactId>
  version>1.0-SNAPSHOTversion>
  packaging>warpackaging>

  name>springmvc02 Maven Webappname>
  
  url>http://www.example.comurl>

  properties>
    project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    maven.compiler.source>13maven.compiler.source>
    maven.compiler.target>13maven.compiler.target>
    
    spring.version>5.0.2.RELEASEspring.version>
  properties>

  dependencies>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-contextartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webmvcartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>javax.servletgroupId>
      artifactId>javax.servlet-apiartifactId>
      version>3.0.1version>
      scope>providedscope>
    dependency>
    dependency>
      groupId>javax.servlet.jspgroupId>
      artifactId>jsp-apiartifactId>
      version>2.0version>
      scope>providedscope>
    dependency>
    dependency>
      groupId>com.fasterxml.jackson.coregroupId>
      artifactId>jackson-coreartifactId>
      version>2.9.10version>
    dependency>
    dependency>
      groupId>com.fasterxml.jackson.coregroupId>
      artifactId>jackson-databindartifactId>
      version>2.9.10version>
    dependency>
    dependency>
      groupId>com.fasterxml.jackson.coregroupId>
      artifactId>jackson-annotationsartifactId>
      version>2.9.10version>
    dependency>
  dependencies>

  build>
    finalName>springmvc02finalName>
    pluginManagement>
      plugins>
        plugin>
          artifactId>maven-clean-pluginartifactId>
          version>3.1.0version>
        plugin>
        
        plugin>
          artifactId>maven-resources-pluginartifactId>
          version>3.0.2version>
        plugin>
        plugin>
          artifactId>maven-compiler-pluginartifactId>
          version>3.8.0version>
          
          configuration>
            source>${maven.compiler.source}source>
            target>${maven.compiler.target}target>
          configuration>
        plugin>
        plugin>
          artifactId>maven-surefire-pluginartifactId>
          version>2.22.1version>
        plugin>
        plugin>
          artifactId>maven-war-pluginartifactId>
          version>3.2.2version>
        plugin>
        plugin>
          artifactId>maven-install-pluginartifactId>
          version>2.5.2version>
        plugin>
        plugin>
          artifactId>maven-deploy-pluginartifactId>
          version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

2、请求页面 

--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/27
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
head>
    title>Titletitle>
    script src="js/jquery.min.js" type="text/javascript">script>
    script>
        $(function() {
            $("#btn").click(function() {
                $.ajax({
                    url:"test",
                    type:"post",
                    //请求数据类型
                    contentType:"application/json;charset=UTF-8",
                    //返回数据类型
                    dataType:"json",
                    //注意json格式字符串的写法
                    data:{"name":"刘阳","age":28},
                    success:function(data) {
                        console.info(data);
                        alert(data.name);
                        alert(data.age);
                    }
                });
            });
        });
    script>
head>
body>
button id="btn">按钮button>
body>
html>

3、处理方法

    /**
     * @ResponseBody 将返回值以json格式返回到页面
     * @RequestBody 自动将RequestBody内容映射到User对象
     * @ResponseBody和@RequestBody需要如下jar包的支持
     * jackson-core
     * jackson-databind
     * jackson-annotations
     */
    @RequestMapping("/test")
    @ResponseBody
    public User test1(@RequestBody User u) {
        System.out.println(u);
        u.setAge(100);
        System.out.println(u);
        return u;
    }

4、总结

4.1、注意ajax请求data的json格式

4.2、处理方法中需使用@RequestBody注解将请求体映射到User对象

4.3、在处理方法上加上@ResponseBody注解将返回值以json格式响应到页面

4.4、@RequestBody和@ResponseBody注解需要jackson-core、jackson-databind、jackson-annotations jar包的支持

发ajax响应json格式数据

标签:content   webapp   coding   ase   ica   compiler   span   creat   lease   

原文地址:https://www.cnblogs.com/liuyang-520/p/12376781.html


评论


亲,登录后才可以留言!