原生js axios请求方式

引入 axios

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

get请求

    //默认请求为get
    axios('http://localhost:5000/Test/g1')
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });




    //无参数,
    axios.get('http://localhost:5000/Test/g1' )
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //无参数,传对象
    axios({
        url:'http://localhost:5000/Test/g1',
        method:'get'
        })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //url上带参请求 
    axios({
        url: 'http://localhost:5000/Test/g2?a=3&str=abc',
        method: 'get'
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //带参
    axios({
        url: 'http://localhost:5000/Test/g2',
        method: 'get',
        //params是URL拼接
        params: {
            a: 3,
            str: 'abc'
        }
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //参数是集合
    axios({
        url: 'http://localhost:5000/Test/g4',
        method: 'get',
        data: [{ "age": 18, "name": "tom" }, { "age": 22, "name": "liu" }]
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

Post

    //无参数
    axios.post('http://localhost:5000/Test/p1')
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //传对象
    axios({
        url: 'http://localhost:5000/Test/p3',
        method: 'post',
        data: {
            age: 18,
            name: 'tom',
            id: 1
        }
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //数组
    axios({
        url: 'http://localhost:5000/Test/p4',
        method: 'post',
        data: ["ab", "cd"]
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //传集合对象
    axios({
        url: 'http://localhost:5000/Test/p5',
        method: 'post',
        data: [{ "age": 18, "name": "tom" }, { "age": 22, "name": "liu" }]
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //传对象和URL参数混合
    axios({
        url: 'http://localhost:5000/Test/p6',
        method: 'post',
        params: { a: 'aaaa', b: 'bbbb' },
        data: [{ "age": 18, "name": "tom" }, { "age": 22, "name": "liu" }]
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //dynamic 对象
    axios({
        url: 'http://localhost:5000/Test/p7',
        method: 'post',
        data: { "age": 18, "name": "tom" }
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });

    //dynamic 数组对象
    axios({
        url: 'http://localhost:5000/Test/p7',
        method: 'post',
        data: [{ "age": 18, "name": "tom" }, { "age": 22, "name": "liu" }]
    })
        .then(function (response) {
            console.log(response.data)
        }).catch(function (err) {
            console.log(err)
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值