本文实例讲述了AngularJS实现ajax请求的方法。分享给大家供大家参考,具体如下:
【HTML 代码】
angularjs实现 ajax
{{user.username}} {{user.email}}
【js代码 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; }); } }]);
【PHP代码 ajax.php】
$user->username, 'email'=>$user->email); //返回数据库 echo json_encode($users);