在angularJS中與遠程HTTP服務器交互時會用一個很是關鍵的服務-$http。javascript
下面進行$http服務的使用說明,調用以下:html
$http(config).success(function(data,status,headers,config){}).error(function(data,status,headers,config){});
二、success爲請求成功後的回調函數,error爲請求失敗後的回調函數,這裏主要是對返回的四個參數進行說明。java
爲了方便你們與HTTP服務器進行交互,angularJS提供了各個請求方式下方法。jquery
$http.put/post(url,data,config) url、name必填,config可選ajax
$http.get/delete/jsonp/head(url,confid) url必填,config可選json
url、data、config與$http的參數一致,瀏覽器
下面有一個simple demo用於展現如何使用$http()及$http.post()。服務器
<!DOCTYPE HTML> <html lang="zh-cn" > <head> <meta charset="UTF-8"> <title>CSSClasses</title> <script src="angular.min.js" type="text/javascript"></script> <script type="text/javascript"> function ctrl($http,$scope){ $scope.login = function(user){ $http.post("login.do",user).success(function(data, status, headers, config){ alert("success"); }).error(function(data, status, headers, config){ alert("error"); }) } $scope.login1 = function(user){ $http({url:"login.do",data:user}).success(function(data, status, headers, config){ alert("success"); }).error(function(data, status, headers, config){ alert("error"); }) } } </script> </head> <body ng-app> <div ng-controller="ctrl"> <form name="loginFm"> Name:<input ng-model="user.name" /> pwd: <input ng-model="user.pwd" /> <input type="button" value="login" ng-click="login(user)" /> <input type="button" value="login1" ng-click="login1(user)" /> </form> </div> </body> </html>