Original link: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angularjs-http-service/
1. General usage:
$http Service is a function that has only one parameter-configuration object, which is used to generate an HTTP request and return a Promise object.
$http ({
method: ' GET ',
URL: '/someurl '
}). Then (function Successcallback (response) {
Successful response
}, function Errorcallback (response) {
Response failed
});
This reply object has these properties:
Data: The response body after the conversion function is converted
Status: HTTP status code for the response
Header: Get the head getter function
Config: The configuration object used to generate the request
StatusText: HTTP status text for the response
The Response state Code (status) is considered a success state between 200-299 and causes the successcallback function to be called.
2. Quick usage:
All shortcut usages need to pass through a URL and require that the data be required by post or put.
$http. Get ('/someurl ', config). Then (Successcallback, Errorcallback);
$http. Post ('/someurl ', data, config). Then (Successcallback, Errorcallback);
Quick usage is:
- $http. Get gets a resource on the server
- $http. Head is similar to get, but only gets HTTP header information
- $http. Post submit data to the server, update create resource, etc.
- $http. Put to transfer Update resources to the specified URI
- $http. Delete to delete a resource
- $http. Jsonp get JSONP data
- $http. Patch to update the content of a section
3. Writing unit tests using $http
When unit tests are performed (with Ngmock), $httpbackend.flush () must be called to clear each pending request.
$httpBackend. Expectget (...);
$http. Get (...);
$httpBackend. Flush ();
4. returns a promise object
var promise= $http ({
Method:' GET ',
URL:"Data.json"
});
Promise.then (function(resp) {
Returns the RESP Response object
},function(resp) {
returns the resp of the error message
});
AngularJS $http Service