Angularjs XMLHttpRequest
$http is a core service in Angularjs that reads data from a remote server.
Read JSON file
The following are the JSON files stored on the Web server:
http://www.runoob.com/try/angularjs/data/Customers_JSON.php
{
"records":
[
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
},
{
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
"Country" : "Spain"
},
{
"Name" : "Galería del gastrónomo",
"City" : "Barcelona",
"Country" : "Spain"
},
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
},
{
"Name" : "Königlich Essen",
"City" : "Brandenburg",
"Country" : "Germany"
},
{
"Name" : "Laughing Bacchus Wine Cellars",
"City" : "Vancouver",
"Country" : "Canada"
},
{
"Name" : "Magazzini Alimentari Riuniti",
"City" : "Bergamo",
"Country" : "Italy"
},
{
"Name" : "North/South",
"City" : "London",
"Country" : "UK"
},
{
"Name" : "Paris spécialités",
"City" : "Paris",
"Country" : "France"
},
{
"Name" : "Rattlesnake Canyon Grocery",
"City" : "Albuquerque",
"Country" : "USA"
},
{
"Name" : "Simons bistro",
"City" : "København",
"Country" : "Denmark"
},
{
"Name" : "The Big Cheese",
"City" : "Portland",
"Country" : "USA"
},
{
"Name" : "Vaffeljernet",
"City" : "Århus",
"Country" : "Denmark"
},
{
"Name" : "Wolski Zajazd",
"City" : "Warszawa",
"Country" : "Poland"
}
]
}
Angularjs $http
The Angularjs $http is a service for reading data on a Web server.
$http. Get (URL) is a function used to read server data.
Angularjs instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
.success(function (response) {$scope.names = response.records;});
});
</script>
</body>
</html>
Run Result:
- alfreds Futterkiste, Germany
- ana Trujillo emparedados y helados, Mexico
- antonio Moreno Taquería, Mexico
around the Horn, UK
- b ' Beverages, UK
- berglunds Snabbköp, Sweden
- blauer to the Delikatessen, Germany
- blondel père et fils France
- bólido Comidas Preparadas, Spain
- bon app., France
- bottom-dollar Marketse, Canada
- cactus Comidas para llevar, Argentina
- centro Comercial Moctezuma, mexic o
- chop-suey Chinese, Switzerland
- comércio Mineiro, Brazil
Application resolution:
Note: The above code GET request is the server of this station, you can not copy directly to your local run, there will be cross-domain problem, the solution is to
Copy the customers_json.php data to your own server with the best solution for PHP Ajax cross-domain problems.
ANGULARJS applications are defined by Ng-app. Application is performed in <div>.
The ng-controller Directive sets the controller object name.
The function Customerscontroller is a standard JavaScript object builder.
The Controller object has an attribute: $scope. Names.
$http. Get () reads static JSON data from the Web server.
The server data file is: http://www.runoob.com/try/angularjs/data/Customers_JSON.php.
When you load JSON data from the server, $scope. Names into an array.
Note: The above code can also be used to read database data.
The above is the ANGULARJS XMLHttpRequest data collation, follow-up continue to add, hope to help needy friends.