源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

解决angular的$http.post()提交数据时后台接收不到参数值问题的方法

  • 时间:2022-03-05 02:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
写此文的背景:在学习使用angular的$http.post()提交数据时,后台接收不到参数值,于是查阅了相关资料,寻找解决办法。 写此文的目的:通过上面提到的文章中的解决之道,结合自己的经验,总结了如下发现。 前端:html,jquery,angular 后端:java,springmvc [b]一、平常使用的post提交和接收方式 [/b]前端使用jquery提交数据。
$.ajax({
  url:'/carlt/loginForm',
  method: 'POST',  
  data:{"name":"jquery","password":"pwd"},
  dataType:'json',
  success:function(data){
    //...
  }
});
后端java接收:
@Controller
public class UserController {
  @ResponseBody
  @RequestMapping(value="/loginForm",method=RequestMethod.POST)
  public User loginPost(User user){
    System.out.println("username:"+user.getName());
    System.out.println("password:"+user.getPassword());
    return user;
  }
}
model(不要忘记get、set方法):
public class User {
  private String name;
  private String password;
  private int age;
  
  //setter getter method

}
后台打印: [b]username:jquery password:pwd[/b] 调用接口查看到的前端返回结果: [img]http://files.jb51.net/file_images/article/201512/2015121090841744.png?201511109849[/img] [b]二、使用angularJs的post方法提交 [/b]
<div ng-app="myApp" ng-controller="formCtrl">
 <form novalidate>
 UserName:<br>
 <input type="text" ng-model="user.username"><br>
 PassWord:<br>
 <input type="text" ng-model="user.pwd">
 <br><br>
 <button ng-click="login()">登录</button>
 </form>
</div>
js代码:
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope,$http) {
 $scope.login = function() {
  $http({
   url:'/carlt/loginForm',
   method: 'POST',   
   data: {name:'angular',password:'333',age:1}  
  }).success(function(){
   console.log("success!");
  }).error(function(){
   console.log("error");
  })
 };
});
后台打印结果: [b]username:null password:null:[/b] 查看前端: [img]http://files.jb51.net/file_images/article/201512/2015121090904937.png?201511109911[/img] [b]三、解决angular提交post问题。 [/b]相信看过上面提到的哪怕文章的人已经知道怎么解决问题了吧。文中是更改了angular的提交方式,使得angular的提交数据方式更像jquery的。 我试过,也是行得通的。然后我又试了另外一种方式。如下: 前端不变,依然是:
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope,$http) {
  $scope.login = function() {
    $http({
      url:'/carlt/loginForm',
      method: 'POST',      
      data: {name:'angular',password:'333',age:1}   
    }).success(function(){
      console.log("success!");
    }).error(function(){
      console.log("error");
    })
  };
});
后台变了,只是在User前加上@RequstBody,因为angular提交的是json对象:
@Controller
public class UserController {
  @ResponseBody
  @RequestMapping(value="/loginForm",method=RequestMethod.POST)
  public User loginPost(@RequestBody User user){
    System.out.println("username:"+user.getName());
    System.out.println("password:"+user.getPassword());
    return user;
  }
}
@RequestBody
[b]作用:[/b]       i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;       ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。 [b]使用时机:[/b] A) GET、POST方式提时, 根据request header Content-Type的值来判断:     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理); B) PUT方式提交时,根据[b]request header Content-Type[/b]的值来判断:     application/x-www-form-urlencoded, 必须;     multipart/form-data, 不能处理;     其他格式,必须; 说明:request的body部分的数据编码格式由header部分的Content-Type指定; [b]四、解决了angular问题之后,发现jquery按照原来的方式提交post请求会报错(错误码415)。[/b] 如下方式可以解决jquery提交问题:
$.ajax({
  url:'/carlt/loginForm',
  method: 'POST',
  contentType:'application/json;charset=UTF-8',
  data:JSON.stringify({"name":"jquery","password":"pwd"}),
  dataType:'json',
  success:function(data){
    //...
  }
});
json对象转json字符串:JSON.stringify(jsonObj); 以上就是本文的全部内容,有兴趣的同学可以试试其它方法,希望本文可以解决大家遇到的angular的post提交问题。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部