赞
踩
本文主要是针对SpringBoot的ajax 请求、响应案例,配合适当注解,方便开发中使用,都是rest接口
$.ajax中contentType 和 dataType
contentType:设置你发送给服务器的格式
dataType:设置你收到服务器数据的格式,json/text
contentType 默认值:“application/x-www-form-urlencoded; charset=UTF-8”
JSON.stringify():将js数据转换为json格式
目录
创建:DataGetRequestController
- @RestController
- public class DataGetRequestController {
-
- }
在controller中写一个int参数绑定的方法
- @GetMapping("/getInt")
- public int getInt(int id){
- return id;
- }
jquery的ajax请求
- $("#getInt").click(function(){
- $.ajax({
- type: "get",
- url: "/getInt?id=1000",
- dataType: "text",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getIntMsg").html(data)
- }
- });
- });
在controller中写多个包装类型参数绑定的方法
- @GetMapping(value = "/getBox",produces = {"application/json;charset=utf-8"})
- public String getBox(String name,Integer id){
- return "name = " + name + " id = " + id;
- }
jquery的ajax请求
- $("#getBox").click(function(){
- $.ajax({
- type: "get",//type:get type:post type:put type:delete
- url: "/getBox?id=1000&name=小王同学",
- dataType: "text",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getBoxMsg").html(data)
- }
- });
- });
在controller中写自定义请求参数
- @GetMapping(value = "/getUser",produces = {"application/json;charset=utf-8"})
- public User getUser(User user){
- return user;
- }
jquery的ajax请求
- $("#getUser").click(function(){
- $.ajax({
- type: "get",//type:get type:post type:put type:delete
- url: "/getUser?userName=小王同学&password=123456&id=10000",
- dataType: "json",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getUserMsg").html("userName:"+data.userName+"password:"+data.password)
- }
- });
- });
在controller中写List<Integer> 请求参数
- @GetMapping("/getIds")
- public List<Long> getIds(@RequestParam List<Long> ids){
- return ids;
- }
jquery的ajax请求
- $("#getIds").click(function(){
- $.ajax({
- type: "get",//type:get type:post type:put type:delete
- url: "/getIds?ids=1,2,3",
- dataType: "json",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getIdsMsg").html(data[0]+","+data[1]+","+data[2])
- }
- });
- });
controller中添加Map实体类参数
- @GetMapping(value = "/getMap",produces = {"application/json;charset=utf-8"})
- public Map<String,Object> getMap(@RequestParam Map<String,Object> map){
- return map;
- }
jquery中的ajax请求
- $("#getMap").click(function(){
- $.ajax({
- type: "get",//type:get type:post type:put type:delete
- url: "/getMap?id=10&name=小王同学&salary=3000",
- dataType: "json",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getMapMsg").html("id="+data.id+",name="+data.name+",salary="+data.salary)
- }
- });
- });
controller增加数组请求参数
- @GetMapping("/getArr")
- public String[] getArr(String[] arr){
- return arr;
- }
jquery中的ajax请求
- $.ajax({
- type: "get",//type:get type:post type:put type:delete
- url: "/getArr?arr=小王同学,滑雪,溜冰",
- dataType: "json",// 设置你收到服务器数据的格式,根据服务器返回类型填:json、text
- //contentType : 'application/json;charset=utf-8',//设置你发送给服务器的格式: application/json;charset=utf-8
- success: function(data){
- console.log(data)
- $("#getArrMsg").html(data[0])
- }
- });
由于前后端分离以及前端的多样性,通常我们使用json数据格式进行参数/数据传递,说到json格式,就得先说一下@RequestBody,这个是使用Json模式进行参数绑定必不可少的一环
1、@RequestBody注解解析
1)@RequestBody注解的作用将json格式的数据转为java对象
2)@RequestBody常用其来处理application/json类型
3)@RequestBody接收的是一个json格式的字符串
在controller中增加基本类型参数
- @PostMapping("/postInt")
- public Integer postInt(Integer id){
- return id;
- }
jquery的ajax请求
- $.ajax({
- type: "post",
- url: "/postInt",
- data: {"id":1000},
- dataType: "json",
- success: function(data){
- console.log(data)
- $("#postIntMsg").html(data)
- }
- });
在controller中增加自定义类型参数
- @PostMapping("/postUser")
- public User postUser(@RequestBody User user){
- return user;
- }
jquery的ajax请求
- $.ajax({
- type: "post",
- url: "/postUser",
- data: JSON.stringify({"userName":"小黑","password":"123456"}),
- dataType: "json",
- contentType : 'application/json;charset=utf-8',
- success: function(data){
- console.log(data)
- $("#postUserMsg").html(data.userName+","+data.password)
- }
- });
在OrderForm类中有Goods类
@Data public class OrderForm { private String sn; private Goods goods; }@Data public class Goods { private Long id; private String goodsName; }
增加OrderForm的controller
- @PostMapping(value = "/postOrderForm",produces = {"application/json;charset=utf-8"})
- public OrderForm postOrderForm(@RequestBody OrderForm orderForm){
- return orderForm;
- }
jquery的ajax请求
- $.ajax({
- type: "post",
- url: "/postOrderForm",
- data: JSON.stringify(
- {
- "sn":"123456789",
- "goods":{"id":10,"goodsName":"华为手机"}
- }
- ),
- dataType: "json",
- contentType : 'application/json;charset=utf-8',
- success: function(data){
- console.log(data)
- $("#postOrderFormMsg").html("sn:"+data.sn+",goodsName="+data.goods.goodsName)
- }
- });
在controller中增加List<Integer> 参数
- @PostMapping("/postIds")
- public List<Integer> postIds(@RequestBody List<Integer> ids){
- return ids;
- }
jquery的ajax请求
- $.ajax({
- type: "post",
- url: "/postIds",
- data: JSON.stringify([1,2,3,4,5]),
- dataType: "json",
- contentType : 'application/json;charset=utf-8',
- success: function(data){
- console.log(data)
- $("#postIdsMsg").html(data[0]+","+data[1])
- }
- });
- @PostMapping("/postUsers")
- public List<User> postUsers(@RequestBody List<User> users){
- return users;
- }
- $.ajax({
- type: "post",
- url: "/postUsers",
- data: JSON.stringify(
- [
- {"userName":"admin","password":"123456"},
- {"userName":"manager","password":"manager"}
- ]),
- dataType: "json",
- contentType : 'application/json;charset=utf-8',
- success: function(data){
- console.log(data)
- $("#postUsersMsg").html(data[0].userName+","+data[1].userName)
- }
- });
- @PostMapping("/postMap")
- public Map<String,Object> postMap(@RequestBody Map<String,Object> map){
- return map;
- }
- $.ajax({
- type: "post",
- url: "/postMap",
- data: JSON.stringify({"name":"小王同学","salary":5000,"age":20}),
- dataType: "json",
- contentType : 'application/json;charset=utf-8',
- success: function(data){
- console.log(data)
- $("#postMapMsg").html(data.name+","+data.salary+","+data.age)
- }
- });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。