AngularJS $http service (send Ajax asynchronous request)


demo.html:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>AngularJS</title>  
    <script src="angular.min.js"></script> <!-- Introduce AngularJS framework-->  
</head>  
<body ng-app="App">  
    <div ng-controller="DemoController">  
    </div>
    <script>  
          
        var App = angular.module('App',[]);  
  
        // $http service (send Ajax asynchronous request)  
        App.controller("DemoController",['$scope','$http',function($scope,$http) {  
            // $http is essentially the encapsulation of the XMLHttpRequest object  
  
            // Send Ajax asynchronous request  
            $http({  
                method: 'post',  
                url: './demo.html',  
                params: {name: 'Zhang San', age: 12}, // The parameters of the get request (different from JQuery, JQuery is placed in data)  
                data: {name: 'Zhang San', age: 12}, // Parameters for post request (AngularJS preferentially supports RESTFUL (not SOAP interface) interface service method (default data is passed in JSON format));  
                //data: "name=Zhang San&age=12", // RESTFUL will process JSON objects on the server side, AngularJS itself does not process JSON objects. (If the server interface is not a RESTFUL interface, then you cannot directly transfer JSON data)  
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}  
            }).success(function(data,status,headers,config) {  
                // ....  
            }).error(function(data,status,headers,config) {  
                // ....  
            });  
  
        }]);  
          
    </script>  
</body>  
</html>  



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948160&siteId=291194637