深入理解Angularjs中$http.post與$.post
摘要
在angularjs發(fā)送post請(qǐng)求的時(shí)候,確實(shí)很困惑,在傳遞json數(shù)據(jù)的時(shí)候,總會(huì)遇到在服務(wù)端無(wú)法接受到參數(shù)的情況,這里有必要與$.post進(jìn)行比較學(xué)習(xí)一下。
一個(gè)例子
這里模擬登錄的一個(gè)場(chǎng)景,post用戶名與密碼,服務(wù)端接受賬戶并直接返回到客戶端不做其它業(yè)務(wù)處理。
使用angularjs版本
/* AngularJS v1.2.15 (c) 2010-2014 Google, Inc. http://angularjs.org License: MIT */
服務(wù)端
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); });
響應(yīng)
這里我們看一下請(qǐng)求體
那么我們現(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)了,處于習(xí)慣的原因,平時(shí)就會(huì)這樣來(lái)寫$http.post的。但結(jié)果并不是想要的。那么咱們與$.post對(duì)比一下請(qǐng)求體。
看到?jīng)]?差別就在這里。
發(fā)現(xiàn)問題了,那么我們就要轉(zhuǎn)化為$.post提交參數(shù)的方式。幸好,angularjs中$http.post提供了一個(gè)轉(zhuǎn)化參數(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ù)轉(zhuǎn)化為$.post提交參數(shù)的方式。這樣看到的請(qǐng)求體中參數(shù)就與$.post相同了。
可以在全局進(jìn)行設(shè)置
<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>
總結(jié)
angularjs在進(jìn)行post請(qǐng)求的時(shí)候要進(jìn)行參數(shù)配置。關(guān)于angularjs的post請(qǐng)求,建議在初始化模塊的時(shí)候?qū)ost請(qǐng)求設(shè)置請(qǐng)求頭與請(qǐng)求參數(shù)轉(zhuǎn)換的設(shè)置,這樣可以在其他地方方便使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決angular的$http.post()提交數(shù)據(jù)時(shí)后臺(tái)接收不到參數(shù)值問題的方法
- 對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- Angularjs中$http以post請(qǐng)求通過消息體傳遞參數(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í)例詳解
- Angular利用HTTP POST下載流文件的步驟記錄
相關(guān)文章
Angular4.0中引入laydate.js日期插件的方法教程
在AngularJs中我們會(huì)不可避免的使用第三方庫(kù),例如jquery插件庫(kù)。下面這篇文章主要給大家介紹了關(guān)于Angular4.0中引入laydate.js日期插件的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12angularJs中跳轉(zhuǎn)到指定的錨點(diǎn)實(shí)例($anchorScroll)
今天小編就為大家分享一篇angularJs中跳轉(zhuǎn)到指定的錨點(diǎn)實(shí)例($anchorScroll),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-08-08AngularJS基礎(chǔ) ng-srcset 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-srcset 指令,這里對(duì)ng-srcset 指令做了詳細(xì)的資料整理,附有代碼示例,有需要的小伙伴可以參考下2016-08-08AngularJS基礎(chǔ) ng-options 指令詳解
本文主要介紹AngularJS ng-options 指令,這里對(duì)ng-options指令的知識(shí)做了詳細(xì)整理,并附有詳細(xì)的代碼示例,有需要的小伙伴可以參考下2016-08-08AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能,涉及AngularJS使用ng-blur、ng-focus針對(duì)表單事件監(jiān)聽相關(guān)操作技巧,需要的朋友可以參考下2017-10-10總結(jié)十個(gè)Angular.js由淺入深的面試問題
這篇文章雖然只有10個(gè)問題,但是覆蓋了angular開發(fā)中的各個(gè)方面,有基本的知識(shí)點(diǎn),也有在開發(fā)過程中遇見的問題,同時(shí)也有較為開放性的問題去辨別面試者的基礎(chǔ)水準(zhǔn)和項(xiàng)目經(jīng)驗(yàn),注意答案僅供參考哦~2016-08-08