jquery post json
2015-10-27 11:53
567 查看
jquery ajax调用,有的情况下提交的参数比较多,这时我们希望将这些参数作为一个json对象传递到controller中进行处理,那么就需要使用到post json对象的功能,下面的例子简单说明这个功能的使用。
js代码,需要注意contentType:”application/json”,不然会出现415的错误
controller代码,需要注意@RequestBody的使用,它和@ResponseBody的作用一样,都是数据json处理
js代码,需要注意contentType:”application/json”,不然会出现415的错误
<script type="text/javascript"> $(function(){ var para = { name : 'lpn', cname : 'shine' }; var jsonpara = $.toJSON(para); $("#checkName").click(function(){ $.ajax({ url : webContextPath + "/main/checkName", async : true, type : "POST", contentType : "application/json", dataType : "json", data : jsonpara, success : function(response) { var errorInfo = response.errorInfo; if (errorInfo != null) { $("#checkInfo").html(errorInfo); } else { $("#checkInfo").html("name is available"); } }, error : function() { alert("server exception"); } }); }); }); </script>
controller代码,需要注意@RequestBody的使用,它和@ResponseBody的作用一样,都是数据json处理
@ResponseBody @RequestMapping(value = "/checkName", method = RequestMethod.POST) public Map<String, Object> checkName(@RequestBody CheckNameParam checkNameParam) { System.out.println(checkNameParam.toString()); Map<String, Object> jsonMap = new HashMap<String, Object>(); return jsonMap; }
相关文章推荐
- jQuery效果之页面窗口及jQuery插件开发
- Jquery选择器
- jQuery.noop
- jquery实现的V字形显示效果代码
- 基于Jquery+div+css实现弹出登录窗口(代码超简单)
- jquery操作HTML5 的data-*的用法实例分享
- jquery 评分插件
- jQuery效果之级联下拉框
- jquery each,click ,元素选择器
- jquery图文列表鼠标点击下拉显示文字说明内容
- jquery arguments
- 了解jQuery
- jQuery中$.get、$.post、$.getJSON和$.ajax的用法详解
- 基于SpringMVC与jquery的ajax提交表单的若干情况详解
- jQuery停止动画和判断是否处于动画状态
- JQM[jquery mobile] 实战经验汇总
- jQuery无刷新分页完整实例代码
- 第33日 我疯了集成平台(六)-步履轻盈JQuery(四)
- 10003---使用jQuery实现Ajax
- jQuery无刷新分页完整实例代码