axios基操

130 阅读1分钟

Axios的基本使用

Axios的网址www.axios-js.com,使用需要先引入axios

Axios是一个专注于网络请求的库!

使用方式

axios({
    method: 'GET',//请求方式
    url: 'https://cnodejs.org/api/v1/topic/5ae140407b0e8dc508cca7cc',//请求地址
    params: {
        id: 1
      },//URL中的查询参数
    }).then(function (result) {
        console.log(result);
    }).catch(function (a) {
        console.log('错误');

        })

结合async 和 await 方式使用:

如果调用某个方法的返回值是promise实例,则前面可以添加await

await 只能被asyns修饰的方法中*

    document.querySelector('#btn').addEventListener('click', async function () {
    let result = await axios({
        method: 'POST',//请求方式
        url: 'https://cnodejs.org/api/v1/topic/5ae140407b0e8dc508cca7cc',//请求地址
        params: {
        name: 'zs',
        age: 20
        }})
        console.log(result);
      })