Axios & fetch api & Promise & POST

本文深入探讨了Axios和Fetch API在Vue.js中的应用,包括如何使用它们进行HTTP请求,特别是POST请求的处理。文章提供了详细的代码示例,展示了如何在Vue组件的mounted生命周期钩子中使用Axios获取数据,并对比了Axios与Fetch API的区别。

Axios & fetch api & Promise & POST

https://github.com/axios/axios

https://appdividend.com/2018/02/20/vue-js-axios-tutorial/#Vue_js_Axios_Post_Example

http://codeheaven.io/how-to-use-axios-as-your-http-client/


$ npm i -S axios

bug

740516-20181113144118662-1162632258.png


    mounted() {
        console.log(`fetch data after mounted lifecycle!`);
        // let url = "http://localhost:8888/preview-user";
        // let url = "./preview-user.js";
        // let url = "./preview-user.json";
        let url = "http://10.1.5.202/es6-test/axios/preview-user.json";
        Axios.get(url)
        // .then(res => res.json())
        .then((res) => {
            let {
                data: json
            } =  res;
            console.log(`json =`, json);
            console.log(`json.data =`, json.data);
            // console.log(`res.data.data =`, res.data.data);
            // console.log(`res =`, res);
            // console.log(`res.message =`, res.message);
            // console.log(`res.data.length =`, res.data.length, JSON.stringify(res.data[0], null, 4));
        });
    },

Axios & Fetch

https://stackoverflow.com/questions/40844297/what-is-difference-between-axios-and-fetch

https://medium.com/@thejasonfile/fetch-vs-axios-js-for-making-http-requests-2b261cdd3af5

https://css-tricks.com/using-data-in-react-with-the-fetch-api-and-axios/


vue & mounted

https://stackoverflow.com/questions/32413905/initializing-vue-data-with-ajax/32443886#32443886

vue & created

https://stackoverflow.com/questions/47635503/fetch-data-with-vue-from-web-api


Ajax

Asynchronous JavaScript And XML

https://en.wikipedia.org/wiki/Ajax_(programming)

pagination

image


let table = new Uint8Array(100).map((item, i) => (item = i));

table.slice(0, 50);
table.slice(1*50, 1*50 + 50);


shit aliyun



fetch(url, {
    body: `nickname=xxxxx&_csrf_token=MD2ToUvQZ-3fk-g-gxczDhwiptfdRBnA57isO7wjBSE`,
    cache: "no-cache",
    credentials: "same-origin",
    // credentials: "include",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "POST",
    mode: "cors",
    redirect: "follow",
    referrer: "no-referrer",
})
.then(res => res.json())
.then(json => {
    // json
    console.log(`json =`, JSON.stringify(json, null, 4));
    return json;
})
.catch(err => console.error(`error =`, err));

shit aliyun

image


fetch(url, {
    body: `nickname=xxxxx&_csrf_token=MD2ToUvQZ-3fk-g-gxczDhwiptfdRBnA57isO7wjBSE`,
    cache: "no-cache",
    credentials: "same-origin",
    // credentials: "include",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "POST",
    mode: "cors",
    redirect: "follow",
    referrer: "no-referrer",
})
.then(res => res.json())
.then(json => {
    // json
    console.log(`json =`, JSON.stringify(json, null, 4));
    return json;
})
.catch(err => console.error(`error =`, err));


转载于:https://www.cnblogs.com/xgqfrms/p/9951878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值