"Go" angular communicate with the server via $http

Source: Internet
Author: User

Http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View

Angular is a front-end framework that implements interactive pages, but for a Web application, where does the data from the page come from, and certainly needs to be supported by the server, how does angular interact with the server?

$httpangular provides the $http service to communicate with the service side, $http the XMLHttpRequest object of the service team browser is encapsulated, allowing us to request data from the server in an AJAX way. The $http service is a function that takes a parameter, the type of the parameter is the object that is used to configure the generated HTTP request, and the function returns a Promise object (for the Promise specification, you can see this article)

var promise = $http ({  method: ' GET ',  URL: '/api/user.json '});p Romise.then (function (resp) {}, function (RESP) {} )
$http the requested Configuration object

$http () Accepted configuration objects can contain the following properties:

Method:http request method, can be get,delete,head,jsonp,post,put

URL: string, target of request

Params: A string or object that is converted to the URL appended by the query string

Data: used when sending a POST request, sent to the server as the body of the message

Headers: A list, each element is a function that returns an HTTP header

Xsrfheadername (String): Name of the HTTP header that holds the XSFR token

Xsrfcookiename: The cookie name that holds the XSFR token

Transformrequest: A function or array of functions used to convert the request body and header information of an HTTP request and return the converted result.

Transformresponse: An array of functions or functions used to convert the response body and header information of an HTTP response and return the converted results.

Cache: The Boolean type or Cache object, after which the angular caches the GET request.

Timeout: Numeric, deferred request

Responsetype: String, response type. Can be Arraybuffer, Blob,document,json, text, Moz-blob, Moz-chunked-text, Moz-chunked-arraybuffer

$http the requested Response object

Angular the response object passed to the then method includes the following properties

Data: The response body after conversion

Status:http Response Status Code

Headers: Header information

Config: The setting object that generated the original request

Statustext:http text of the response status

Interception device

Angular through interceptors we can intercept requests and responses from a global level.

Before using interceptors, we declare a service through factory () and then register the interceptor via $httpprovider. Interceptors are divided into four types, two successful interceptors and two failure interceptors.

Angular.module (' Test ', []). Factory (' Testinterceptor ', function ($q) {var interceptor = {  ' request ': function ( Config) {    return config;  },  ' response ': function (resp) {    return response;  },  ' Requesterror ' : function (rejection) {    return $q. Reject (rejection);  },  ' Responseerror ': function (rejection) {    return rejection  }} return interceptor;}) Angular.module (' Test ', []). config (function ($httpProvider) {$httpProvider. Interceptors.push (' Testinterceptor ');})

from:http://blog.csdn.net/yangnianbing110/article/details/43124679

"Go" angular communicate with the server via $http

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.