AngularJS Http
AngularJS XMLHttpRequest
$httpIt is a core service in AngularJS used to read data from a remote server.
Usage format:
Original prompt (Chinese, left untranslated)
// 简单的 GET 请求,可以改为 POST
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// 请求成功执行代码
}, function errorCallback(response) {
// 请求失败执行代码
});
Abbreviated method
POST and GET abbreviated method format:
$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
There are also the following abbreviated methods:
- $http.get
- $http.head
- $http.post
- $http.put
- $http.delete
- $http.jsonp
- $http.patch
For more details, see:https://docs.angularjs.org/api/ng/service/$http
Read JSON file
The JSON file stored on the web server is:
https://www.runoob.com/try/angularjs/data/sites.php
{
"
sites
"
:
[
{
"
Name
"
:
"
Beginner Tutorial
"
,
"
Url
"
:
"
www.runoob.com
"
,
"
Country
"
:
"
CN
"
}
,
{
"
Name
"
:
"
"
,
"
Url
"
:
"
www.google.com
"
,
"
Country
"
:
"
USA
"
}
,
{
"
Name
"
:
"
"
,
"
Url
"
:
"
www.facebook.com
"
,
"
Country
"
:
"
USA
"
}
,
{
"
Name
"
:
"
"
,
"
Url
"
:
"
www.weibo.com
"
,
"
Country
"
:
"
CN
"
}
]
}
AngularJS $http
AngularJS $http is a service used to read data from a web server.
$http.get(url) is a function used to read server data.
Deprecation Notice (v1.5)
In v1.5
$httpthesuccessanderrormethods are deprecated. Usethenmethod instead.
Generic Method Instance
AngularJS 1.5 and above - Instance
var
app
=
angular
.
module
(
'
myApp
'
,
[
]
)
;
app
.
controller
(
'
siteCtrl
'
,
function
(
$
scope
, $
http
)
{
$
http
(
{
method
:
'
GET
'
,
url
:
'
https://www.runoob.com/try/angularjs/data/sites.php
'
}
)
.
then
(
function
successCallback
(
response
)
{
$
scope
.
names
=
response
.
data
.
sites
;
}
,
function
errorCallback
(
response
)
{
//
Execute code on request failure
}
)
;
}
)
;
Try it »
Shorthand method examples
AngularJS version 1.5 and above - Example
<
div
ng-app
=
"
myApp
"
ng-controller
=
"
siteCtrl
"
<
ul
<
li
ng-repeat
=
"
x in names
"
{{ x.Name + ', ' + x.Country }}
</
li
</
ul
</
div
<
script
var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http.get("https://www.runoob.com/try/angularjs/data/sites.php") .then(function (response) {$scope.names = response.data.sites;}); });
</
script
Try it »
AngularJS 1.5 and earlier versions - Example
<
div
ng-app
=
"
myApp
"
ng-controller
=
"
siteCtrl
"
<
ul
<
li
ng-repeat
=
"
x in names
"
{{ x.Name + ', ' + x.Country }}
</
li
</ul>
<ul>
</ul>
<div>
<
<script
var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http.get("https://www.runoob.com/try/angularjs/data/sites.php") .success(function (response) {$scope.names = response.sites;}); });
</
script
Try it »
Application parsing:
Note: The get request in the code above is from this site's server. You cannot copy it to run locally as it will cause cross-domain issues. The solution is to copy the data from Customers_JSON.php to your own server. Attached: Best solution for PHP Ajax cross-domain issues
AngularJS applications are defined byng-appThe application runs in
ng-controllerThe directive setsthe controller objectname.
The functioncustomersControlleris a standard JavaScript **object constructor**.
The controller object has a property:$scope.names.
$http.get()Read staticJSON data from the web server..
The server data file is: ** https://www.runoob.com/try/angularjs/data/sites.php**
When JSON data is loaded from the server,$scope.namesBecomes an array.
![]() |
The above code can also be used to read database data. |
|---|
