ajax url 多参数,用ajax 调用api来 get数据的url带有参数的优化 ?

各位大神,

事情是这样的, 目前要调用api来get数据,

但是这个api的endpoiont里头带有参数, 而这些参数是无序的

所以说, 必须先get到参数, 再将他们传到目标url里面, 才抓到的目标的数据

状况大概是这样的:

我目标是要做一个列表, 列出同学们的身高、体重、姓名

这个api是长这样的

/students/?studentid={studentid}

---

[{

name: '王小明',

height: '175cm',

weight: 60

}]

因为有一个班有50位同学, 所以我必须先从另一个端点用50次请求得到这50位同学的id,

再分别将id 传入 这个url 再做 50次请求, 得到一个一个同学的资讯, 再分别将这些资讯push到一个array(students) 中, 使这个array拥有50位同学的资讯(50笔)

(vue)

---

this.$http.get(`/students/?studentid=${1~50}`).then(

response => {

this.students.push(各个 response.information)

},

response => {

console.log('error...')

}

)

我本来打算使用for回圈遍历, 但这样请求数爆炸多......

目前寫在promise回調內

getStudent () {

this.$http.get(api.student_list).then(studentresponse => {

studentresponse.data.forEach(student => {

this.student_ids.push(student.id)

})// 取得student_id的array

this.student_ids.forEach(id => {

this.$http.get(api.student_info + `?studentid=${id}`).then(

inforesponse => {

this.student_infos.push(inforesponse.data[0])

console.log(this.student_infos)

},

inforesponse => {

console.log('error')

}

)// 這id array傳入info端點取的資料

})

}, studentresponse => {

console.log('error')

})

}

(慘不忍睹呀)

而且如果之后有一个端点需要更多的参数, 那肯定会完蛋,现在在电脑前面不知道如何是好......

在网上查优化好久都查不到结果, 请问有什么效能佳的方法或是思路吗?

或是針對我的寫法有任何見解的都請大大提出了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值