Implementing 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": "[email protected]"},{' username ': "zhangsan2", "email": "[ Email protected] "},{' username ':" zhangsan3 "," email ":" [email protected] "}];*/$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//gets the 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 this simplifies inserting the database $users[] = Array (' username ' = = $user->username, ' email ' = + $user->email);//Return to database Echo Json_encode ($users);
Angularjs one of the Learning notes (Ajax request)