Axios
Axios is a Promise-based HTTP client dedicated to browsers and node. JS Services
Vue 2.0 officially recommends the use of Axios instead of the original Vue request, so here is a description of Axios's function and basic usage, and I hope all of you can help. ^_^
Function
- Sending xmlhttprequests requests in the browser
- Sending an HTTP request in node. js
- Support Promise API
- Intercept requests and responses
- Transform request and response data
- Cancel Request
- Automatically convert JSON data formats
- Client support to protect against XSRF attacks
Browser support
Axios can support more than IE7 versions of IE, while supporting most mainstream browsers, it is important to note that your browser needs to support Promise to be able to use Axios. So it's a good idea to install Polyfill before using Axios.
Installation
Using NPM:
$ npm install axios
Using Bower:
$ bower install axios
Using CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Use
Here's a Vue example: After installing Axios in NPM, you need to refer to the package in the Main.js file
import axios from ‘axios‘
And then globally bind
Vue.prototype.$http = axios
You can then use it in the. vue file $http
instead ofaxios
GET
Make a request for a user with a given ID Axios.Get'/user?id=12345 '). Then (function (response) {Console.log (response);}). catch (function (error) {Console.log (error);}); //Optionally the request above could also is done as Axios. get ('/user ', {params: {ID: 12345}}). Then (function (response) {Console.log (response);}).
catch (
function (error) {Console.log (error);});
POST
axios.post(‘/user‘, { firstName: ‘Fred‘, lastName: ‘Flintstone‘ }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
Send multiple requests at the same time
< Span class= "Hljs-keyword" >function getuseraccount () {return Axios. get ( '/user/12345 ');} function getUserPermissions< Span class= "Hljs-params" > () {return axios. Get ( '/user/12345/permissions ');} Axios.all ([Getuseraccount (), Getuserpermissions ()]). Then (Axios.spread (function (acct, perms) {//Both Requests is now Complete});
Of course, Axios features include Axios API, Interceptor, and so on, here you want to know more about the official documents: Axios, followed by the introduction of the use of interceptor and the configuration of various parameters.
Copyright NOTICE: This work is licensed under the Creative Commons Attribution-NonCommercial use-no derivative of the 3.0 non-localized version license agreement. 77650059
Basic introduction and use of Axios (GET and POST)