axios

110 阅读1分钟

基本使用

GET请求

axios({
    //请求类型
    method: 'GET',
    // URL
    url: 'http://localhost:3000/posts/2'
}).then(res => {
    console.log(res)
})

如果get请求有参数可以写成,实际上传递的是http://localhost:3000/posts/2?a=100&b=200

axios({
    //请求类型
    method: 'GET',
    // URL
    url: 'http://localhost:3000/posts/2',
    params: {
        a: 100,
        b: 200
    }
}).then(res => {
    console.log(res)
})

POST请求

axios({
    //请求类型
    method: 'POST',
    // URL
    url: 'http://localhost:3000/posts',
    //设置请求体
    data: {
        title: "今天天气不错",
        author: "张三"
    }
}).then(res => {
    console.log(res)
})

PUT请求

axios({
    //请求类型
    method: 'PUT',
    // URL
    url: 'http://localhost:3000/posts/3',
    //设置请求体
    data: {
        title: "今天天气不错",
        author: "张三"
    }
}).then(res => {
    console.log(res)
})

DELETE请求

axios({
    //请求类型
    method: 'DELETE',
    // URL
    url: 'http://localhost:3000/posts/3',
    //设置请求体
    data: {
        title: "今天天气不错",
        author: "张三"
    }
}).then(res => {
    console.log(res)
})