AngularJS發(fā)送異步Get/Post請(qǐng)求方法
1、在頁(yè)面中加入AngularJS并為頁(yè)面綁定ng-app 和 ng-controller
<body ng-app="MyApp" ng-controller="MyCtrl" > ... <script src="js/angular.min.js"></script> <script src="js/sbt.js"></script>
2、添加必要的控件并綁定相應(yīng)的事件
get:<input type="text" ng-model="param">{{param}} <br> post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br> <button ng-click="get()">Get</button> <button ng-click="post()">Post</button>
3、在JS腳本中發(fā)送進(jìn)行Get/Post請(qǐng)求
get
$scope.get = function () { $http.get("/get", {params: {param: $scope.param}}) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
get 將參數(shù)放在URL中
$scope.get = function () { $http.get("/get?param="+$scope.param) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
post
$scope.post = function () { $http.post("/post", $scope.user) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
4、由Controller處理請(qǐng)求并返回結(jié)果
get
@RequestMapping("/get") @ResponseBody public Map<String,String> get(String param) { System.out.println("param:"+param); response.put("state", "success");//將數(shù)據(jù)放在Map對(duì)象中 return response; }
post
@RequestMapping("/post2") @ResponseBody public void post2(@RequestBody User user, HttpServletResponse resp) { //返回不同的http狀態(tài) if(user.getName()!=null&&!user.getName().equals("")){ resp.setStatus(200); } else{ resp.setStatus(300); } }
如果需要配置請(qǐng)求頭部
$http({ method : "POST", url : "/post", data : $scope.user }).success(function(data, header, config, status) { console.log(data); }).error(function(data, header, config, status) { console.log(data); });
5、由JS http請(qǐng)求的回調(diào)函數(shù)處理并執(zhí)行下一步操作
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Request</title> </head> <body ng-app="MyApp" ng-controller="MyCtrl" > get:<input type="text" ng-model="param"><br> post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br> <button ng-click="get()">Get</button> <button ng-click="post()">Post</button> </body> <script src="js/angular.min.js"></script> <script src="js/sbt.js"></script> </html>
sbt.js
var app = angular.module("MyApp", []); app.controller("MyCtrl", function ($scope, $http) { $scope.get = function () { $http.get("/get", {params: {param: $scope.param}}) .success(function (data, header, config, status) { console.log(data); }) .error(function (response) { console.log(response); }) ; } $scope.post = function () { $http.post("/post", $scope.user) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; } });
以上這篇AngularJS發(fā)送異步Get/Post請(qǐng)求方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過濾字符查找功能
這篇文章主要介紹了 Bootstrap + AngularJS 實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)過濾字符查找功能,代碼簡(jiǎn)單易懂,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2017-07-07Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法
今天小編就為大家分享一篇Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-09-09Angular?服務(wù)器端渲染錯(cuò)誤消息localStorage?is?not?defined解決分析
這篇文章主要為大家介紹了Angular?服務(wù)器端渲染錯(cuò)誤消息localStorage?is?not?defined解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07angularJS利用ng-repeat遍歷二維數(shù)組的實(shí)例代碼
本篇文章主要介紹了angularJS利用ng-repeat遍歷二維數(shù)組的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Angularjs中的事件廣播 —全面解析$broadcast,$emit,$on
下面小編就為大家?guī)?lái)一篇Angularjs中的事件廣播 —全面解析$broadcast,$emit,$on。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧2016-05-05使用AngularJS 應(yīng)用訪問 Android 手機(jī)的圖片庫(kù)
這篇文章主要介紹了使用AngularJS 應(yīng)用訪問 Android 手機(jī)的圖片庫(kù)的相關(guān)資料,需要的朋友可以參考下2015-03-03RequireJS 依賴關(guān)系的實(shí)例(推薦)
下面小編就為大家?guī)?lái)一篇RequireJS 依賴關(guān)系的實(shí)例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-01-01AngularJS基礎(chǔ) ng-mousemove 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-mousemove 指令,這里幫大家整理了ng-mousemove 指令的詳細(xì)資料,并附有示例代碼,有需要的朋友參考下2016-08-08