axios的基础语法
axios({
methid:'请求的类型',
url:'请求的URL地址',
}).then((result) => {
})
发起 GET 请求:
axios({
method: 'GET',
url: 'http://www.liulongbin.top:3006/api/getbooks',
params: {
id: 1
}
}).then(function (result) {
console.log(result)
})
发起 POST 请求:
document.querySelector('#btnPost').addEventListener('click', async function () {
const { data: res } = await axios({
method: 'POST',
url: 'http://www.liulongbin.top:3006/api/post',
data: {
name: 'zs',
age: 20
}
})
console.log(res)
})
结合async和await
- 如果调用某个方法的返回值是Promise实例,则前面可以添加await
- await只能在被async修饰的方法中