Angular $http Service Detailed

Source: Internet
Author: User
Tags http post

It is a simple encapsulation of native XMLHttpRequest objects,

This method returns a Promise object with a sccess and error two methods.

Of course, we can also use the then method when the response is returned and get a special parameter that represents the object's success or failure information.

Alternatively, you can use the success and error callbacks instead.

$http ({url:url,//URL Path of the requestMethod:method,//Get/delete/head/jsonp/post/putParams:params,//into the form of param1=xx1¶m2=xx2.Data:data//contains data that will be sent to the server as the body of the message, which is typically used when the POST request}}). Success (function(response, status, header, config, statustext) {//Successful processing}). Error (function(data,header,config,status) {//Error Handling});

When we use $http as a function, i.e. $http (), we need to pass in an object that can contain the following keys:

1. Method string indicates the type of request sent get post Jsonp and so on

2, URL string absolute or relative URL, the target of the request

3. Params string or object will be converted into a query string appended to the URL, if not the string will be serialized by JSON

4, the data string or object This object contains information sent to the server as the message body, generally used in the post request, and starting from angular1.3 can be sent in the POST request binary data

such as var blob = New blob ({name: ' Zhang San '}), $http ({method: ' Get ', url: '/', data:blob});

5, headers objects when we do the post cross-domain and background mates use the headers, which represents the HTTP header string sent with the request

6. Xsrfheadername string Save the name of the HTTP header of the XSFR token

7. Xsrfcookiename the name of the cookie that holds the XSFR token for the string

8. The Transformrequest function or function group is used to convert the HTTP request header and body information and return the converted version, which is typically used to serialize the request before it is sent to the server

9. The Transformresponse function or function group is used to convert the HTTP response header and the response body information, and returns the converted version, which is typically used for deserialization

10. Cache Boolean or Cache object if set to true angularjs The GET request is cached with the default $http cache

11, Timout Value or Promise object, if it is a value then the request will end in the specified milliseconds (will jump to the failed error method), if the object is Promise object is resolve when the request is aborted, the method executes the request

12. Responsetype String This option sets the Xmlhttpresponsetype property in the request to the following types:

"" String default, "Arraybuffer" (Arraybuffer), "Blob" (Blob object), "document" (HTTP document), "JSON" (JSON string parsed from JSON object),

"Text" (string), "Moz-blob" (Firefox receive Progress event), "Moz-chunked-text" (text stream), "Moz-chunked-arraybuffer" (Arraybuffer stream)

$http provide some quick ways for us to use, a total of six (in fact, six kinds of request mode)

1. $http. Get (URL string, config Optional configuration-object type) returns the Httppromise object

2, $http. Delete (URL string, config Optional configuration-object type) returns the Httppromise object

3. $http. Head (URL string, config Optional configuration-object type) returns the Httppromise object

4, $http. Jsonp (url string, config Optional configuration-object type) return Httppromise object

5. $http. Post (URL string, data object or String, config optional configuration-object type) returns the Httppromise object

6. $http. Put (URL string, data object or String, config optional configuration-object type) returns the Httppromise object

Submitting data data with angular $http post differs from jquery's $.ajax ():

Ajax Default Request Header Content-type is application/x-www-form-urlencoded, this data to the control layer can be used @requestparam, @ModelAttribute, @ Requestbody can handle it.

Angular $http default is Application/json, this data to the control layer must be handled by @requestbody

Description: The data encoding format of the body part of request is specified by the Content-type of the header section;

Angular $http Service Detailed

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.