栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot获取前台传递对象的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot获取前台传递对象的方法

SpringBoot获取前台传递对象的方法

SpringBoot获取前台传递对象并不容易,稍有错误就会报错。这里提供一个大致模板方法:

前台代码:

let student = {
  id: 1,
  name: 'tom',
  email: '123@qq.com',
  age: 15
};

$('#pojoTest').click(function () {
  $.ajax({
    url: getContextPath() + `/test/test3`,
    type: "post",
    data: JSON.stringify(student),
    contentType: "application/json;charset=utf-8",
    success: function (result) {
        console.log(result);
    },
  });
});

后台代码:

@PostMapping("/test3")
public AjaxResult test3(@RequestBody Student student) {
  System.out.println(student);
  return AjaxResult.ok("姓名:" + student.getName());
}

注意细节:

  • 必须使用post请求方式
  • 前台对象转为字符串对象
  • 后台使用@RequestBoby注解
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842980.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号