java端使用注解接收参数时,ajax请求注意
2017-10-19 16:22
447 查看
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}) @ResponseBody public 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") @ResponseBody public 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; }
相关文章推荐
- java使用axios.js的post请求后台时无法接收到参数的问题
- Java使用Apache POI导出-注意前端不要使用Ajax异步请求
- Java中可变长参数的使用及注意事项
- JQuery中使用Ajax传中文参数乱码解决方案(javaweb开发)
- 解决Ajax请求参数名称为中文接收报DataFormatException
- Java中可变长参数的使用及注意事项
- 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
- 如何使用Ajax(XMLHttpRequest)发送带参数的请求,以及如何在Servlet中获取请求中的参数
- java web 学习笔记四 使用ajax响应异步请求
- Java中可变长参数的使用及注意事项
- session失效,使用ajax请求数据被拦截,此时正常的处理逻辑是跳到登录界面,而不是界面没有变化(java判断是否是ajax请求)
- Java中可变长参数的使用及注意事项
- 掌握 Ajax,第 2 部分: 使用 JavaS 4000 cript 和 Ajax 发出异步请求
- Java中可变长参数的使用及注意事项
- Spring MVC 中使用ajax发送POST请求时的参数问题
- java adf 使用ajax传递参数中有中文乱码问题(转)
- Java中可变长参数的使用及注意事项
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)
- php学习笔记(三十)ajax请求和接收参数的实现方式(包括json数据格式的简单处理)
- Java中可变长参数的使用及注意事项