Common applications and parameters of the $ http service in AngularJS, and angularjs Parameters
Preface
$ Http service: it simply encapsulates the nativeXMLHttpRequest
An object that contains the configuration content used to generate an HTTP request. This function returnspromise
Objectsuccess
Anderror
Method.
$ Http service scenarios:
Var promise = $ http ({method: "post", // It can be get, post, put, delete, head, jsonp; get, posturl :". /data. json ", // Request Path params: {'name': 'lisa '}, // transfer the parameter, string map or object, and convert it? Name = lisa follows the Request Path in the form of data: blob, // usually used when a post request is sent, binary data is sent, and blob Object is used .}). Success (function (data) {// response successful operation}). error (function (data) {// response failed (response returned in error status) operation })
then()
Function: You can usethen()
Function to process the $ http service callback,then()
The function accepts two optional functions as parameters, indicatingsuccess
Orerror
You can also usesuccess
Anderror
Callback substitution:
then(successFn, errFn, notifyFn)
, Regardlesspromise
Succeeded or failed. When the result is available,then
Will be called asynchronously immediatelysuccessFn
OrerrFn
. This method always uses a parameter to call the callback function: the result or the reason for rejection.
Inpromise
Before being executed or rejected,notifyFn
The callback may be called 0 to multiple times to provide a prompt about the Process status.
Promise. then (function (resp) {// call upon successful response, resp is a response object}, function (resp) {// call upon failed response, resp has error message });
then()
The resp (Response object) received by the function contains five attributes:
1. data (string or object ):Response body
2. status:Http status code, such as 200
3. headers (function ):The getter function of the header information. A parameter can be used to obtain the value of the corresponding name.
4. config (object ):Generate the complete setting object of the original request
5. statusText:Corresponding http status text, such as "OK"
Or usesuccess/error
Method, use
// The promise is successfully processed. success (function (data, status, headers, config) {// response to successful processing}); // error handling promise. error (function (data, status, headers, config) {// handle unsuccessful responses });
Instance used:
Index.html
<! Doctype html>
App. js
var myHttpApp = angular.module("myApp",[]);myHttpApp.controller("myAppController",function($q,$http,$scope){ var deffer = $q.defer(); var data = new Blob([{ "name":"zhangsan" }]) $scope.loadData = function(){ var promise = $http({ method:"post", url:"./data.json", cache: true }).success(function(data){ deffer.resolve(data); }).error(function(data){ deffer.reject(data); }) promise.then(function(data){ $scope.myData = data.data; }) /*promise.success(function(data){ $scope.myData = data; })*/ }})
Data. json
[ {"name":"zhangsan","attr":"China"}, {"name":"lisa","attr":"USA"}, {"name":"Bob","attr":"UK"}, {"name":"Jecy","attr":"Jepan"}]
Result:
Callthen()
Returnsresp
Object:
Summary
In AngularJS, the commonly used applications and parameters of the $ http service have basically ended. I hope this article will help you learn how to use AngularJS. If you have any questions, leave a message.