Home > Web Front-end > JS Tutorial > body text

How to implement ajax request in AngularJS

高洛峰
Release: 2016-12-06 09:34:14
Original
1364 people have browsed it

The example in this article describes how AngularJS implements ajax requests. Share it with everyone for your reference, as follows:

【HTML code】




  
  
  
  angularjs实现 ajax

{{user.username}} {{user.email}}
Copy after login

【js code ajax.js】

var myModule = angular.module("HelloAjax",[]);
myModule.controller("HelloAjax",["$scope","$http",function HelloAjax($scope,$http){
  /*
  $scope.users=[{'username':"zhangsan","email":"zs@11.com"},
    {'username':"zhangsan2","email":"zs@22.com"},
    {'username':"zhangsan3","email":"zs@33.com"}];
  */
  $scope.get_more = function(){
    $http({
        method: "POST",
        url: "./ajax.php",
        data:{'username':$scope.username,
           'email':$scope.email
          }
      }).
      success(function(data, status) {
       //$scope.status = status;
        $scope.users = data;
      }).
      error(function(data, status) {
       //$scope.data = data || "Request failed";
       //$scope.status = status;
     });
   }
}]);
Copy after login

【PHP code ajax.php】

$user->username,
         'email'=>$user->email);
//返回数据库
echo json_encode($users);
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!