解決angular的$http.post()提交資料時後臺接收不到引數值問題的方法
寫此文的背景:在學習使用angular的$http.post()提交資料時,後臺接收不到引數值,於是查閱了相關資料,尋找解決辦法。
寫此文的目的:通過上面提到的文章中的解決之道,結合自己的經驗,總結了如下發現。
前端:html,jquery,angular
後端:java,springmvc
一、平常使用的post提交和接收方式
前端使用jquery提交資料。
1 2 3 4 5 6 7 8 9 |
$.ajax({
url: '/carlt/loginForm' ,
method:
'POST' ,
data:{ "name" : "jquery" , "password" : "pwd" },
dataType: 'json' ,
success: function (data){
//...
}
});
|
後端java接收:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@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
}
|
後臺列印:
username:jquery
password:pwd
呼叫介面檢視到的前端返回結果:
二、使用angularJs的post方法提交
1 2 3 4 5 6 7 8 9 10 |
< 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程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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的。
我試過,也是行得通的。然後我又試了另外一種方式。如下:
前端不變,依然是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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物件:
1 2 3 4 5 6 7 8 9 10 11 |
@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請求的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提交問題:
1 2 3 4 5 6 7 8 9 10 |
$.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提交問題。