深入理解Angularjs中$http.post與$.post
摘要
在angularjs發(fā)送post請求的時候,確實很困惑,在傳遞json數(shù)據(jù)的時候,總會遇到在服務端無法接受到參數(shù)的情況,這里有必要與$.post進行比較學習一下。
一個例子
這里模擬登錄的一個場景,post用戶名與密碼,服務端接受賬戶并直接返回到客戶端不做其它業(yè)務處理。
使用angularjs版本
/* AngularJS v1.2.15 (c) 2010-2014 Google, Inc. http://angularjs.org License: MIT */
服務端
public class AccountController : Controller
{
// GET: /<controller>/
public IActionResult Login()
{
return View();
}
[HttpPost]
public IActionResult Login(string userName,string userPwd)
{
var resut = Request.Form;
return Json(new { _code = 200, _msg = "Login success", name = userName, password = userPwd });
}
}
$.post
首先使用$.post的方式,直接提交賬戶密碼
$.post("@Url.Content("~/Account/Login")",{ userName: "2342342", userPwd:"2sssdfs" },function (data) {
console.log(data);
});
響應

這里我們看一下請求體

那么我們現(xiàn)在看看angularjs的$http.post的情況,到底區(qū)別在哪兒?
@{
Layout = null;
}
<!DOCTYPE html>
<html ng-app="login">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>IT怪O 用戶登錄</title>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="external nofollow" rel="stylesheet" />
<script src="~/js/angular.min.js"></script>
<script>
angular.module("login", []).controller("LoginController", function ($http, $scope) {
$scope.Login = function () {
var data = { userName: $scope.userName, userPwd: $scope.userPwd };
var config = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
//transformRequest: function (obj) {
// var str = [];
// for (var p in obj) {
// str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
// }
// return str.join("&");
//}
};
console.log(data);
$http.post("@Url.Content("~/Account/Login")", data, config).success(function (data) {
console.log(data);
});
};
});
</script>
</head>
<body>
<div ng-controller="LoginController">
<input type="text" placeholder="用戶名" ng-model="userName" value="" />
<input type="password" placeholder="密碼" ng-model="userPwd" value="" />
<button ng-click="Login()">登錄</button>
</div>
</body>
</html>
登錄

出現(xiàn)了,處于習慣的原因,平時就會這樣來寫$http.post的。但結果并不是想要的。那么咱們與$.post對比一下請求體。

看到?jīng)]?差別就在這里。
發(fā)現(xiàn)問題了,那么我們就要轉化為$.post提交參數(shù)的方式。幸好,angularjs中$http.post提供了一個轉化參數(shù)的transformRequest方法,可以在config中加上該參數(shù):
var config = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function (obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
};
它的作用就是將提交的參數(shù)轉化為$.post提交參數(shù)的方式。這樣看到的請求體中參數(shù)就與$.post相同了。
可以在全局進行設置
<script>
angular.module("login", []).controller("LoginController", function ($http, $scope) {
$scope.Login = function () {
var data = { userName: $scope.userName, userPwd: $scope.userPwd };
console.log(data);
$http.post("@Url.Content("~/Account/Login")", data).success(function (data) {
console.log(data);
});
};
}).config(function ($httpProvider) {
$httpProvider.defaults.transformRequest = function (obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
};
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
});
</script>
總結
angularjs在進行post請求的時候要進行參數(shù)配置。關于angularjs的post請求,建議在初始化模塊的時候?qū)ost請求設置請求頭與請求參數(shù)轉換的設置,這樣可以在其他地方方便使用。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 解決angular的$http.post()提交數(shù)據(jù)時后臺接收不到參數(shù)值問題的方法
- 對比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- Angularjs中$http以post請求通過消息體傳遞參數(shù)的實現(xiàn)方法
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- AngularJS下$http服務Post方法傳遞json參數(shù)的實例
- AngularJS $http模塊POST請求實現(xiàn)
- AngularJS $http post 傳遞參數(shù)數(shù)據(jù)的方法
- angularJS 發(fā)起$http.post和$http.get請求的實現(xiàn)方法
- AngularJS封裝$http.post()實例詳解
- Angular利用HTTP POST下載流文件的步驟記錄
相關文章
AngularJS實現(xiàn)動態(tài)編譯添加到dom中的方法
這篇文章主要介紹了AngularJS實現(xiàn)動態(tài)編譯添加到dom中的方法,結合實例形式分析了AngularJS動態(tài)編輯構建模板的相關操作技巧,需要的朋友可以參考下2016-11-11
Angularjs編寫KindEditor,UEidtor,jQuery指令
使用過 AngularJS 的朋友應該最感興趣的是它的指令。現(xiàn)今市場上的前端框架也只有AngularJS 擁有自定義指令的功能,并且AngularJS 是目前唯一提供Web應用可復用能力的框架。2015-01-01
AngularJs根據(jù)訪問的頁面動態(tài)加載Controller的解決方案
這篇文章主要介紹了AngularJs根據(jù)訪問的頁面動態(tài)加載Controller的解決方案,需要的朋友可以參考下2015-02-02
angularjs實現(xiàn)多選框分段全選效果實現(xiàn)
這篇文章主要為大家介紹了angularjs實現(xiàn)多選框分段全選效果實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
angular.js4使用 RxJS 處理多個 Http 請求
本篇文章主要介紹了angular.js使用 RxJS 處理多個 Http 請求,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
AngularJS解決ng界面長表達式(ui-set)的方法分析
這篇文章主要介紹了AngularJS解決ng界面長表達式(ui-set)的方法,通過具體問題的分析并結合實例形式給出了AngularJS長表達式的相關使用技巧,需要的朋友可以參考下2016-11-11

