解決angular的$http.post()提交數(shù)據(jù)時(shí)后臺(tái)接收不到參數(shù)值問(wèn)題的方法
寫(xiě)此文的背景:在學(xué)習(xí)使用angular的$http.post()提交數(shù)據(jù)時(shí),后臺(tái)接收不到參數(shù)值,于是查閱了相關(guān)資料,尋找解決辦法。
寫(xiě)此文的目的:通過(guò)上面提到的文章中的解決之道,結(jié)合自己的經(jīng)驗(yàn),總結(jié)了如下發(fā)現(xiàn)。
前端:html,jquery,angular
后端:java,springmvc
一、平常使用的post提交和接收方式
前端使用jquery提交數(shù)據(jù)。
$.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
}
后臺(tái)打?。?/p>
username:jquery
password:pwd
調(diào)用接口查看到的前端返回結(jié)果:

二、使用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");
})
};
});
后臺(tái)打印結(jié)果:
username:null
password:null:
查看前端:

三、解決angular提交post問(wèn)題。
相信看過(guò)上面提到的哪怕文章的人已經(jīng)知道怎么解決問(wèn)題了吧。文中是更改了angular的提交方式,使得angular的提交數(shù)據(jù)方式更像jquery的。
我試過(guò),也是行得通的。然后我又試了另外一種方式。如下:
前端不變,依然是:
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");
})
};
});
后臺(tái)變了,只是在User前加上@RequstBody,因?yàn)閍ngular提交的是json對(duì)象:
@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
作用:
i) 該注解用于讀取Request請(qǐng)求的body部分?jǐn)?shù)據(jù),使用系統(tǒng)默認(rèn)配置的HttpMessageConverter進(jìn)行解析,然后把相應(yīng)的數(shù)據(jù)綁定到要返回的對(duì)象上;
ii) 再把HttpMessageConverter返回的對(duì)象數(shù)據(jù)綁定到 controller中方法的參數(shù)上。
使用時(shí)機(jī):
A) GET、POST方式提時(shí), 根據(jù)request header Content-Type的值來(lái)判斷:
application/x-www-form-urlencoded, 可選(即非必須,因?yàn)檫@種情況的數(shù)據(jù)@RequestParam, @ModelAttribute也可以處理,當(dāng)然@RequestBody也能處理);
multipart/form-data, 不能處理(即使用@RequestBody不能處理這種格式的數(shù)據(jù));
其他格式, 必須(其他格式包括application/json, application/xml等。這些格式的數(shù)據(jù),必須使用@RequestBody來(lái)處理);
B) PUT方式提交時(shí),根據(jù)request header Content-Type的值來(lái)判斷:
application/x-www-form-urlencoded, 必須;
multipart/form-data, 不能處理;
其他格式,必須;
說(shuō)明:request的body部分的數(shù)據(jù)編碼格式由header部分的Content-Type指定;
四、解決了angular問(wèn)題之后,發(fā)現(xiàn)jquery按照原來(lái)的方式提交post請(qǐng)求會(huì)報(bào)錯(cuò)(錯(cuò)誤碼415)。
如下方式可以解決jquery提交問(wèn)題:
$.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對(duì)象轉(zhuǎn)json字符串:JSON.stringify(jsonObj);
以上就是本文的全部?jī)?nèi)容,有興趣的同學(xué)可以試試其它方法,希望本文可以解決大家遇到的angular的post提交問(wèn)題。
- 對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- Angularjs中$http以post請(qǐng)求通過(guò)消息體傳遞參數(shù)的實(shí)現(xiàn)方法
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- AngularJS下$http服務(wù)Post方法傳遞json參數(shù)的實(shí)例
- AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn)
- AngularJS $http post 傳遞參數(shù)數(shù)據(jù)的方法
- angularJS 發(fā)起$http.post和$http.get請(qǐng)求的實(shí)現(xiàn)方法
- AngularJS封裝$http.post()實(shí)例詳解
- 深入理解Angularjs中$http.post與$.post
- Angular利用HTTP POST下載流文件的步驟記錄
相關(guān)文章
AngularJS集合數(shù)據(jù)遍歷顯示的實(shí)例
下面小編就為大家分享一篇AngularJS集合數(shù)據(jù)遍歷顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
angularJs中跳轉(zhuǎn)到指定的錨點(diǎn)實(shí)例($anchorScroll)
今天小編就為大家分享一篇angularJs中跳轉(zhuǎn)到指定的錨點(diǎn)實(shí)例($anchorScroll),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
詳談Angular 2+ 的表單(一)之模板驅(qū)動(dòng)型表單
這篇文章主要介紹了Angular 2+ 的表單(一)之模板驅(qū)動(dòng)型表單,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
Angular ui.bootstrap.pagination分頁(yè)
這篇文章主要為大家詳細(xì)介紹了Angular ui.bootstrap.pagination 分頁(yè)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
angular 實(shí)現(xiàn)同步驗(yàn)證器跨字段驗(yàn)證的方法
幾乎每個(gè)web應(yīng)用都會(huì)用到表單,那么驗(yàn)證器就是必不可少的東西,這篇文章主要介紹了angular 實(shí)現(xiàn)同步驗(yàn)證器跨字段驗(yàn)證的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

