Use $http shortcut method to interact with the server
In AngularJS, the interaction between the page and the server mainly involves calling modules.
Depending on the request type, the $http module provides different calling methods. Its general format is as follows.
Parameter explanation:
url: Indicates a relative or absolute server request path;
Request type: includes There are six common request methods: POST, GET, JSONP, DELETE, PUT, and HEAD. Among them, POST and PUT type requests can send data through the optional parameter data, and can also set the data passed during the request through the optional parameter config.
When the $http request is successful, you can obtain the data and related information returned by the server in the success method of the callback.
data: Indicates the parameter return body, usually the result set returned by the request.
status: Indicates the status value returned after the request.
headers: Indicates the header file returned after the request, used to display the header information of the returned request.
config: is an object through which the complete configuration information when sending an HTTP request can be obtained.
Use $http configuration object to interact with the server
Above we introduced the process of using the /$http shortcut to interact with the server. Although this method is simple, it lacks flexibility in configuration and requires a lot of code. In response to this situation, we can use the \$http service template as a function, treat all configuration items that construct the XHR object as an object, and define the object as the formal parameter of the function. When calling, only need to modify the object Each attribute value is sufficient. The specific calling format is as follows.
$http({ method: //表示请求方式,是字符串,常有POST、GET、JSONP、DELETE、PUT、HEAD六种方式 url: //表示向服务器请求的地址 data: //是一个对象,在使用POST/PUT时,该对象将作为消息体的一部分发给服务端 params: //是字符串或对象,发送HTTP请求时,如果是对象,将自动按json格式进行序列化,并追加到url后面,作为发送数据的一部分,传递给服务器。 transformRequest://对请求体信息和请求体进行序列化转换,并生成一个数组发送给服务端。 transformResponse://对相应体头信息和相应体进行反序列化转换,其实质就是解析服务器发送来的被序列化后的数据。 cache://布尔值(true/false),表示是否对http请求返回的数据进行缓存,如果设置为true,则表示需要缓存。 timeout://表示延迟http请求的时间,单位是毫秒。})
For example:
Requirement description:
Add a text box button to the page. , when the user enters a number in the text box and clicks the button, the $http function is called to send an HTTP request to the server, verify the parity of the number, and display the verification result in the page element.
<!DOCTYPE html><html ng-app="a7_3"><head> <meta charset="UTF-8"> <title>使用$http配置对象方式与服务端交互</title> <script src="../script/angular.min.js"></script> <link href="Css/css7.css" rel="stylesheet" ></head><body> <p class="frame" ng-controller="c7_3"> <p class="show"> <input type="text" ng-model="num"> <button ng-click="onclick()">验证奇偶</button> <p class="tip">您输入的是:{{result}}</p> </p> </p> <script type="text/javascript"> angular.module('a7_3',[]) .controller('c7_3',function($scope,$http){ $scope.num = 0; $scope.result = "偶数"; $scope.onclick = function(){ $http({ method: 'GET', url: 'data/chk.php', params:{ n: $scope.num } }).success(function(data,status,headers,config){ $scope.result = data; }) } }); </script> </body> </html>
Analysis:
In the js code of this example, when the user clicks the button, the onclick method bound to the button is triggered. In this method, the $http service is called , and pass parameters to the function in the form of configuration objects, such as method, url and other attribute values.
Because the GET request is used, the value in the text box is passed to the server in the form of key/value through the params attribute. .
In this example, the final content of the requested URL is
htpp://localhost/Ch7/data/chk.php?n=87, where n is the key name and 87 is the key value, which is the text box The number entered in .
When the /$http function sends an HTTP request, you can use the successes method to obtain the data content and other header information returned by the server. For example, data is the returned data, which is the number entered by the user in the text box.
In Angular, after executing the /$http function, its return content is actually a promise object. Therefore, you can directly call the then method through chain writing to obtain success and exception data.
The following two pieces of code have the same function.
$http({//配置对象}) .succes(fn1) .error(fn2)
is equivalent to
$http({//配置对象 }) .then(fn1,fn2)
fn1 and fn2 respectively represent the return functions of request success and error.
Although the functions of both are the same. However, using the then method can receive the complete response object from the server, while the success and error methods only receive the parsed and processed response object, which means that the return object obtained by the then method is more original and complete.
The above is the detailed content of $http service content in AngularJS. For more information, please follow other related articles on the PHP Chinese website!