java端使用注解接收参数时,ajax请求注意
来源:互联网 发布:知乎 人体工程学椅子 编辑:程序博客网 时间:2024/05/15 01:36
springMVC项目中,后台Java方法使用注解获取参数,ajax请求时分两种情况 {1:后台使用@requestParam 2:后台使用@requestBody时}
前端:
1:后台使用@requestParam时需要注意的是: 1》:ajax中参数需要将json对象转成json格式的字符串 2》:contentType需要设置成 application/json;function testAjax() { var url = "/agentmobileApp/test/testRequestBody"; var reqPara = JSON.stringify({'did': '55', 'name': 'jack', 'age': 15}); $.ajax({ async: false, url: url, type: 'POST', timeout: '30000', data: reqPara, contentType: 'application/json;charset=utf-8', dataType: 'json', success: function (rep) { alert(rep.result + " ;" + rep.message + " ;" + rep.data.resp.name); }, error: function (rep) { alert("请检查网络是否连接" + rep); } });}2:后台使用@requestBody时 需要注意的是: 1》:参数为json对象 2》:contentType 选项需要注释掉function testAjax1() { var url = "/agentmobileApp/test1/testResponseBody"; var reqPara = {'did': '555', 'name': 'jack', 'age': 15}; $.ajax({ async: false, url: url, type: 'POST', timeout: '30000', data: reqPara, //contentType: 'application/json;charset=utf-8', dataType: 'json', success: function (rep) { alert(rep.result + " ;" + rep.message + " ;" + rep.data.resp.name); }, error: function (rep) { alert("请检查网络是否连接" + rep); } });}
Java端
/** * 测试responseBody注解返回接送数据 */@RequestMapping("/testRequestBody")//@RequestMapping(value = "/testRequestBody",method = {RequestMethod.POST})@ResponseBodypublic ResultJson testResponseBody(@RequestBody BodyVO bodyVO) { ResultJson resultJson = new ResultJson(); TestVO testVO = new TestVO(); String did = bodyVO.getDid(); String name = bodyVO.getName(); int age = bodyVO.getAge(); logger.info("获取前端的参数did :" + did + " name: " + name + " age: " + age);// 简单组装参数 testVO.setAge(20); testVO.setName(did); testVO.setAddress(did + "66666"); resultJson.setResult("0000"); resultJson.setMessage("成功了啊"); resultJson.getData().put("resp", testVO); return resultJson;}/** * 测试responseBody注解返回接送数据 */@RequestMapping("/testResponseBody")@ResponseBodypublic ResultJson testResponseBody(@RequestParam String did, @RequestParam String name, @RequestParam int age) { ResultJson resultJson = new ResultJson(); TestVO testVO = new TestVO(); testVO.setAge(20); testVO.setName(did); testVO.setAddress(did + "66666"); resultJson.setResult("0000"); resultJson.setMessage("成功了"); resultJson.getData().put("resp", testVO); return resultJson;}
阅读全文
0 0
- java端使用注解接收参数时,ajax请求注意
- Java接收请求参数
- http请求时服务端接收参数所用注解类型
- springmvc中@requestbody注解接收请求参数
- ajax /get 请求,服务器端接收参数乱码
- java使用axios.js的post请求后台时无法接收到参数的问题
- ajax请求参数传递问题,注意!!!
- Spring Mvc+ajax发送请求参数接收问题
- springmvc接收ajax请求参数遇到的坑
- Java使用Apache POI导出-注意前端不要使用Ajax异步请求
- 请求参数接收
- stuct2接收请求参数
- struts2 请求参数接收
- action接收请求参数
- Struts2接收请求参数
- Struts2 请求参数接收
- Spring接收请求参数:
- Struts2_请求参数接收
- 人脸实时情绪与性别识别
- 深入理解分布式事务,高并发下分布式事务的解决方案
- java 后台获取访问客户端ip地址
- PhotonServer游戏服务器部署及日志输出
- 关于”echarts-JSON请求数据”一文读者反映chart无法显示数据的分析
- java端使用注解接收参数时,ajax请求注意
- MySQL逻辑层的两层结构
- 跟着姜少学Java基础编程之八:循环结构
- eclipse hibernate 从PostgreSQL数据库自动生成实体类
- vue介绍
- 关于HTML 代码注入,XSS攻击问题解决
- 阿里云ApsaraCache的正式开源,为什么不能仅仅满足于商业上的成功?
- 搭建mysql集群+keepalived高可用
- 做java的你,这些英文单词都掌握了吗?