AngularJS $http Service

Source: Internet
Author: User

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

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.