Data Interaction Axios

Source: Internet
Author: User

In Vue, we could not intentionally introduce jquery for Web requests, before the Vue had vue-resource to solve the network request problem, and then the official suggested using Axios, while Vue-resource was no longer maintained.

For a detailed tutorial on Axios, see GitHub Address:
Https://github.com/axios/axios

Installation:
NPM Install Axios
Or
<script src= "Https://unpkg.com/axios/dist/axios.min.js" ></script>

Use:

First, the configuration file type:

Axios (config). Then (res=>{to-do}). Catch (err=>{to-do})

Instructions for Get and post requests

        //Get:Axios ({method:' GET ', URL:' Http://www.zhouxiaohouer.com/api/getsth ', params:{name:' Braised chicken rice with yellow sauce ', Price:34}}). Then (res={console.log (Res.data)}).Catch(err=>{Console.log (Err)})//Post:Axios ({method:' POST ', URL:' Http://www.zhouxiaohouer.com/api/poststh ', data:{name:' Braised chicken rice with yellow sauce ', Price:34}, headers: {' Content-type ': ' application/x-www-form-urlencoded '}, Transformrequest: [function(data, headers) {//converts the data format of the data, typically returning a serialized string                    //a QS module is encapsulated within the Axios and can be converted directly after introduction                    returndata; }],}). Then (res={console.log (Res.data)}).Catch(err=>{Console.log (Err)})

Second, the alias type:

Axios.get (url[, config]) axios.post (url[, data[, Config])

With a part of the code in the Vue component

Import Axios from ' Axios 'Import QS from' Qs 'Getgoods () {var_this = This              vardata ={filter:'-_id-__v ',} axios.post (' Http://www.zhouxiaohouer.com/api/poststh ', Qs.stringify (data), {headers: {' Content-type ': ' application/x-www-form-urlencoded '}}). Then (res={_this.msg=json.stringify (Res.data)}, Err={Console.log (ERR)})} 

In Axios, we can also configure the default global configuration in advance

Axios.defaults.baseURL = ' Https://www.zhouxiaohouer.com/api 'axios.defaults.headers.post[' Content-type '] = ' application/x-www-form-urlencoded '

Data Interaction Axios

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.