Axios 类似于for循环发送批量请求{:axios.all axios.spread}。

Axios的请求都是异步的!不能用for循环遍历去批量发送请求

那如果我们需要类似与这样的请求怎么办呢

for(let i =0;i<array.length;i++){
  axios.post(contentPath + 'invoice/new/updatetitle', JSON.stringify(sendTaitol), {
                headers: {
                    "Content-TYpe": "application/json;charset=utf-8"
                }
     });
}

Axios官方也是支持的

主要是:axios.all 方法和 axios.spread方法的运用

let requestArray = new Array();//建立一个存储需要发送的请求
requestArray.push(this.bingfaSendRequestByPhone());//为请求数组添加具体的请求
requestArray.push(this.bingfaSendRequestByTital());
requestArray.push(this.bingfaSendRequestByTuiSong());
axios.all(requestArray).then(
    axios.spread((...resp) => {//可变 ...扩展运算符将数组变成一个参数序列
        let flagByRequest = true;//标志位初始化定制false
        let flagByRequestIndex = "";//失败数据
        [...resp].forEach((item, index) => {
            if (!item.data.success) {
                flagByRequest = false;
                flagByRequestIndex += index + ",";
            }
        });
        //如果都是成功的就跳转
        if (flagByRequest) {
            console.log(flagByRequestIndex);//失败的请求下标索引
        }

    })
).catch(error => {
    console.log(error)
});

具体的请求数组添加的内容形式如下Demo

 let sendPhone = this.localUpdateDataToDo.SendPhoneData;
            axios.post(contentPath + 'invoice/new/acceptModAcct', sendPhone, {
                headers: {
                    "Content-TYpe": "application/json;charset=utf-8"
                }
            });

posted on   白嫖老郭  阅读(1119)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示