Описание тега angular-http
The $http
service is a core Angular service that facilitates communication with the remote HTTP
servers via the browser's XMLHttpRequest
object or via JSONP
.
For unit testing applications that use $http service, see $httpBackend mock.
For a higher level of abstraction, please check out the $resource service.
The $http
API is based on the deferred/promise APIs exposed by the $q service. While for simple usage patterns this doesn't matter much, for advanced usage it is important to familiarize yourself with these APIs and the guarantees they provide.
General usage
The $http
service is a function which takes a single argument — a configuration object — that is used to generate an HTTP
request and returns a promise with two $http specific methods: .then
and .catch
.
// Simple GET request example :
$http.get('/someUrl')
.then(function(response) {
var data = response.data;
// this callback will be called asynchronously
// when the response is available
}).catch(function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'})
.then(function(response) {
// this callback will be called asynchronously
// when the response is available
}).catch(function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Since the returned value of calling the $http
function is a promise, you can also use the then method to register callbacks, and these callbacks will receive a single argument – an object representing the response. See the API signature and type info below for more details.
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest
will transparently follow it, meaning that the error callback will not be called for such responses.