Axios学习笔记

44 阅读1分钟

axios --笔记

//get请求
btn.onclick = function(){
    axios({
        method:'GET',
        url: 'http://localhost:3000/'
    }).then(response){
        console.log(response)
    }
}

//post添加
btn.onclick = function(){
    axios({
        method: 'POST',
        url: 'http://localhost:3000/posts',
        data: {
            ....
        },
    }).then(response){
        console.log(response)
    }
}

//PUT更新
btn.onclick = function(){
    axios({
        method: 'PUT',
        url: 'http://localhost:3000/posts/3',
        data: {
            ....
        },
    }).then(response){
        console.log(response)
    }
}

//Delete删除
btn.onclick = function(){
    axios({
        method: 'DELETE',
        url: 'http://localhost:3000/posts/3'
    }).then(response){
        console.log(response)
    }
}

//通用方式
btn.click = function(){
    axios({
        method: '具体类型',
        url: '',
        baseURL: '',
        transformRequest:  //预处理发送的请求参数,
        transformRequest: //预处理返回的结果,
        headers:  //请求头信息
        params: {},
        timeout:  //请求的超时时间
        ......
    })
}
axios.defaults.baseURL = '....'
//也可以创建不同的axios对象实例,然后借由这些实例去调用不同的接口
//可以利用axios的CancelToken将请求取消