用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')
            })
        }

(慘不忍睹呀)

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

在网上查优化好久都查不到结果, 请问有什么效能佳的方法或是思路吗?
或是針對我的寫法有任何見解的都請大大提出了

阅读 3.4k
1 个回答

为什么发这么多请求呢。想办法用几次请求去查询得到。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题