Angularjs the way to implement AJAX requests _angularjs

Source: Internet
Author: User

The examples in this article describe the Angularjs method for implementing AJAX requests. Share to everyone for your reference, specific as follows:

"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
//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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.