解決angular的$http.post()提交資料時後台接收不到參數值問題的方法_AngularJS

來源:互聯網
上載者:User

寫此文的背景:在學習使用angular的$http.post()提交資料時,後台接收不到參數值,於是查閱了相關資料,尋找解決辦法。

寫此文的目的:通過上面提到的文章中的解決之道,結合自己的經驗,總結了如下發現。
前端:html,jquery,angular
後端:java,springmvc
一、平常使用的post提交和接收方式
前端使用jquery提交資料。

$.ajax({  url:'/carlt/loginForm',  method: 'POST',    data:{"name":"jquery","password":"pwd"},  dataType:'json',  success:function(data){    //...  }});

後端java接收:

@Controllerpublic 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}

後台列印:

username:jquery
password:pwd

調用介面查看到的前端返回結果:

二、使用angularJs的post方法提交

<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");  }) };});

後台列印結果:

username:null
password:null:

查看前端:

三、解決angular提交post問題。
相信看過上面提到的哪怕文章的人已經知道怎麼解決問題了吧。文中是更改了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對象:

@Controllerpublic 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

作用:

      i) 該註解用於讀取Request請求的body部分資料,使用系統預設配置的HttpMessageConverter進行解析,然後把相應的資料繫結到要返回的對象上;

      ii) 再把HttpMessageConverter返回的對象資料繫結到 controller中方法的參數上。

使用時機:

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方式提交時,根據request header Content-Type的值來判斷:

    application/x-www-form-urlencoded, 必須;
    multipart/form-data, 不能處理;
    其他格式,必須;
說明:request的body部分的資料編碼格式由header部分的Content-Type指定;

四、解決了angular問題之後,發現jquery按照原來的方式提交post請求會報錯(錯誤碼415)。

如下方式可以解決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提交問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.