Basic introduction and use of Axios (GET and POST)

Source: Internet
Author: User
Tags creative commons attribution

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)

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.