1、发起 GET 请求:
axios({
// 请求方式
method: 'GET',
// 请求的地址
url: '请求地址',
// URL 中的查询参数
params: {
//请求携带的参数
id: 1
}
}).then(function (result) {
console.log(result)
})
2、发起 POST 请求:
document.querySelector('#btnPost').addEventListener('click', async function () {
// 如果调用某个方法的返回值是 Promise 实例,则前面可以添加 await!
// await 只能用在被 async “修饰”的方法中
const { data: res } = await axios({
method: 'POST',
url: '路径',
data: {
//发送请求携带的参数
name:"张三"
}
})
console.log(res)
})