Angularjs learning notes (ajax request), angularjsajax
Implement ajax requests
[HTML code]
<! DOCTYPE html>
[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; }); }}]);
PHP code ajax. php]
<? Php // obtain the parameter $ data = file_get_contents ("php: // input"); $ user = json_decode ($ data ); // query the database $ conn = mysql_connect ("localhost", "root", ""); mysql_select_db ("test"); $ SQL = "select username, email from users "; $ res = mysql_query ($ SQL, $ conn); $ users = array (); while ($ row = mysql_fetch_assoc ($ res) {$ users [] = $ row ;} // Of course, the insertion of the database $ users [] = array ('username' => $ user-> username, 'email '=> $ user-> email) is simplified ); // return the database echo json_encode ($ users );