通常表单submit传的是 json对象
ajax 传的是json对象
angular传的是json字符串
对于后台接收json对象不需要再参数上加@RequestBody
而对于接收json字符串 需要加@RequestBody,并且要引入jackjson jar包
(1)angular向后台传json字符串@RequestBody
$http({ method:'POST',url:"http://localhost:8090/angu_demo/test.chtm",data:{"age":20 } }) .success(function(data,header,config,status){ //响应成功 $scope.names = data[0].age; }).error(function(data,status){ //处理响应失败 });
后台spring mvc代码
@RequestMapping("/test.chtm") @ResponseBody public List<UserBean> test(@RequestBody UserBean userBean,Model model,HttpServletRequest request,HttpServletResponse response){ int age2=userBean.getAge(); userBean.setAge(age2+1); List<UserBean>list=new ArrayList<UserBean>(); list.add(userBean); return list; }
(2)angular向后台传递json对象
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; var transform =function(data){return $.param(data); } $http.post("http://localhost:8090/angu_demo/test.chtm",{"age":20 },{headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},transformRequest: transform}) .success(function(data,status){ //处理响应失败 });
@RequestMapping("/test.chtm") @ResponseBody public List<UserBean> test( UserBean userBean,HttpServletResponse response){ int age2=userBean.getAge(); userBean.setAge(age2+1); List<UserBean>list=new ArrayList<UserBean>(); list.add(userBean); return list; }
下载地址 http://download.csdn.net/detail/u013378306/9672103 原文链接:https://www.f2er.com/angularjs/148613.html