The examples in this article describe the Angularjs method for implementing AJAX requests. Share to everyone for your reference, specific as follows:
"HTML Code"
"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
//Get parameter
$data = file_get_contents ("Php://input");
$user = Json_decode ($data);
Query 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 it simplifies inserting the database
$users [] = Array (' username ' => $user->username,
' email ' => $user->email);
Returns the database
echo json_encode ($users);
More interested readers of ANGULARJS related content can view the site topics: "Angularjs Introduction and Advanced Course", "jquery common Plug-ins and Usage summary", "jquery Ajax Usage Summary", "JavaScript Ajax Operating Skills summary" and A summary of Php+ajax techniques and applications
I hope this article will help you to Angularjs program design.