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.0</modelVersion> <groupId>com.ly.mvc</groupId>
<artifactId>springmvc02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>springmvc02 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
<!--锁定spring版本-->
<spring.version>5.0.2.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.10</version>
</dependency>
</dependencies> <build>
<finalName>springmvc02</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</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>Title</title>
<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格式数据的更多相关文章

  1. springmvc4.0配置ajax请求json格式数据

    1.导入相关jar包:jackson-annotation-2.5.4.jar,jackson-core-2.5.4.jar,jackson-databind-2.5.4.jar. 2.spring- ...

  2. SpringMvc+ajax 实现json格式数据传递

    传JSON对象 前端 function test () { var param = {username : "yitop"}; $.ajax({ timeout : 20000, ...

  3. ajax 提交 json格式数据到后台

    例子:$.ajax({ type: 'POST', url: "/ROOT/modify.do", contentType: "application/json" ...

  4. jQuery中使用Ajax获取JSON格式数据示例代码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...

  5. var dataObj=eval("("+data+")");//转换为json对象(解决在ajax返回json格式数据的时候明明正确的获取了返回值但是却就是进不去success方法的问题。格式错误)

    一,原理 1.1,解析1 将字符串解析为JavaScript代码,比如:var a = "alert('a');";这里的a就只是一个字符串而已,输出的话也是alert(a);这句 ...

  6. 用springmvc的@RequestBody和@ResponseBody 接收和响应json格式数据

    1.controller @Controller @RequestMapping("/rest/v1") public class WelcomeController { @Req ...

  7. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_8 响应json数据之响应json格式数据

    springMvc的框架已经帮我们做好了.发过来的数据转换为javaBean对象 发过来的键值的形式,如果属性和javaBean对应的话,可以直接封装到对象中. key做额外的转换的时候,需要另外的j ...

  8. jQuery调用ajax获取json格式数据

    <body> <div>点击按钮获取音乐列表</div> <input type="button" id="button&quo ...

  9. ajax中json格式数据如何朝后端发送数据

随机推荐

  1. Typora常用快捷键

    目录 无序列表:输入-之后输入空格 有序列表:输入数字+"."之后输入空格 任务列表:-[空格]空格 文字 标题:ctrl+数字 表格:ctrl+t 生成目录:按回车 选中一整行: ...

  2. SVM(1)模式识别课堂笔记

    引言:当两类样本线性可分时,针对我们之前学习的感知机而言,存在多个超平面能将数据分开,这里要讨论什么样的分类面最好的问题.为此,我们形式化的定义了最优分类超平面,他有两点特征:1.能将训练样本没有错误 ...

  3. Dwz/Jquery--使用Ajax提交表单时调用表单设置的校验

    案例 今天有一个需求就是点击按钮时,使用ajax方式提交表单,而且不是直接用form表单下的submit按钮提交,表单中用的校验是dwz 自带的校验方式,表单模板如下: <li><d ...

  4. DISCUZ 如何为主题帖列表页添加头像,显示发帖者头像

    只显示名字的代码 ```php<em style=" font-size:14px;"> <!--{if $thread['authorid'] &&am ...

  5. 用什么库写 Python 命令行程序?看这一篇就够了

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  6. linux启动Firefox报错,及解决方法

    今天在安装Firefox时,出现如下错误 [root@localhost ~]# firefox XPCOMGlueLoad error for file /usr/lib64/firefox/lib ...

  7. nginx的四个主要组成部分

    1.nginx二进制可执行文件 · 由各模块源码编译出的一个文件 2.nginx.conf配置文件 · 控制nginx的行为 3.access.log访问日志 . 记录每一条http请求信息 4.er ...

  8. 机器学习在IC设计中的应用(二)--根据GBA时序结果来预测PBA

    本文转自:自己的微信公众号<集成电路设计及EDA教程> <机器学习在IC设计中的应用(二)--根据GBA时序结果来预测PBA> AOCV AOCV全称:Advanced OCV ...

  9. It is possible and safe to monitor a table DML history on sqlserver

    He is my test step: In a test enviroument, I make a table "test"/ demo table:create table ...

  10. sqlserver datatime value plus random number

    If we want to make some identiity value in sqlserver , we can use identity data type in a table.Howe ...