Reprinted from: Http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View
Reprinted from: Http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View
Reprinted from: Http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View
Important things say three times, respect the original author, transfer only notes need to be easily accessible.
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 ');})
[Reprint]angular communicates with the server via $http