Contents of articles
First, install Axios. Second, use axios1. Introduce axios2. Get method. Third, post method
summary
preface
we all know that the front end needs to request some data and services asynchronously from the back end, which is the so-called Ajax technology. and Vue.js It has no Ajax capability, so it needs to use Axios to complete the Ajax request.
1、 Install Axios
Axios is a promise based HTTP library, which can be used in browsers and websites node.js Medium
Using NPM:
$ npm install axios
Using yarn:
$ yarn add axios
note: install in the root directory of Vue project, and select the configuration file of the project package.json With the corresponding version number, the installation is successful
2、 Using Axios
1. Introducing Axios
stay main.js When the file is injected into Axios:
import axios from 'axios'
Vue.prototype.$axios = axios //The $axios before the equal sign can be replaced with another name, but use it consistently
2. Get method
a relatively complete format: </ font>
this.$axios({ //The $axios here is the one named in main.js
method: "get",
url: "http://wthrcdn.etouch.cn/weather_mini",//an API for querying city weather
headers:{
'Content-type': "application/json;charset=utf-8"
},
params : {
city : "GuangZhou"
},
dataType:'json',
})
.then((res)=>{
console.log(res.data);
console.log("success")
})
.catch(function (error) {
console.log(error);
alert("Failed to connect server");
console.log("fail")
});
3. Post method
a relatively complete format: </ font>
this.$axios({
method:"post",
url:"http://test/...",
headers:{
'Content-type': "application/json;charset=utf-8"
},
data : {
a : '',
b : ''
},
dataType:'json',
})
.then((res)=>{
console.log(res.data);
console.log("success")
})
.catch(function (error) {
console.log(error);
alert("Failed to connect server");
console.log("fail")
});
summary
for Axios, it is not too difficult to use without studying its principle. And through the above get and post methods, we can also find that its syntax is very similar to jQuery’s Ajax, and we can easily use it.
Read More:
- Mac: How to installs docker through brew
- Win10 installs ubuntu system, error WslRegisterDistribution failed with error: 0x8007019e
- How to Use pychar to run tensorflow and virtualenv (How to install tensorflow)
- How to Install Mongdb4.X in Windows
- How to solve the error of “failed to create the Java virtual machine” in installing eclipse 2020-03 in MAC environment
- How to uninstall Maya on a Mac
- How to Set latex text Bold & Italic
- How To Install WordPress with LAMP on Ubuntu 16.04
- How to Install FFmpeg on Linux
- FreeSWITCH installation error “You must install libyuv-dev to build mod_fsv” solution
- Macos: How to Solve cocoapods Install Error
- How to Solve Cython-bbox pip install Error
- Install the mxnet-gpu version to solve the problem of “OSError: libnccl.so.2” when importing
- How to install PIL in Python 3
- How to Revert Your File & Folder by “FOUND.000”
- Use Maven install to type the war package, which shows that the package XXX does not exist
- How To Install Java with Apt-Get on Ubuntu 16.04
- How to Solve program install error 0x80070660
- Solution to the problem of installing Tkinter module in MAC
- How to Install MongoDB Community Edition Manually on mac and linux