Common applications and parameters of the $ http service in AngularJS, and angularjs Parameters

Source: Internet
Author: User

Common applications and parameters of the $ http service in AngularJS, and angularjs Parameters

Preface

$ Http service: it simply encapsulates the nativeXMLHttpRequestAn object that contains the configuration content used to generate an HTTP request. This function returnspromiseObjectsuccessAnderrorMethod.

$ 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, indicatingsuccessOrerrorYou can also usesuccessAnderrorCallback substitution:

then(successFn, errFn, notifyFn) , RegardlesspromiseSucceeded or failed. When the result is available,thenWill be called asynchronously immediatelysuccessFnOrerrFn. This method always uses a parameter to call the callback function: the result or the reason for rejection.

InpromiseBefore being executed or rejected,notifyFnThe 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/errorMethod, 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()ReturnsrespObject:

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.

Related Article

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.