小编Kie*_* Vu的帖子

如何在多个页面上获取数据?

我的项目基于React,redux,redux-saga,es6,我尝试从此API提取数据:

http://api.dhsprogram.com/rest/dhs/data/BD,2000,2004,2007?&returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json

如您所见,此特定的API调用显示的数据限制为每页100个数据,分布在40个页面上。

根据此答案: http ://userforum.dhsprogram.com/index.php?t=msg&th=2086&goto=9591&S=Google它表示您可以将限制扩展到每页最多3000个数据。

但是,在某些情况下,我会进行超出该限制的API调用,这意味着我不会像这样接收所有数据:

export function fetchMetaData(countryCode: string, surveyYears: string) {
return (fetch('http://api.dhsprogram.com/rest/dhs/data/' + countryCode + ',' + surveyYears + '?returnFields=CharacteristicLabel,Indicator,IndicatorId,Value&f=json')
    .then(response => response.json())
    .then(json => json.Data.map(survey => survey)))
} 
Run Code Online (Sandbox Code Playgroud)

所以我的问题是;考虑到我知道数据的总页数,从此API获取所有数据的最佳方法是什么。论坛链接中的答案建议循环浏览API。但是,我找不到正确的语法用法来做到这一点。

我的想法是进行一次api调用以获取页面总数。然后使用redux + redux-saga将其存储在状态中。然后执行一个新请求,将总页数作为参数发送,并获取此总页数。通过这样做,我无法弄清楚存储每次迭代数据的语法。

javascript fetch reactjs redux redux-saga

1
推荐指数
2
解决办法
4578
查看次数

标签 统计

fetch ×1

javascript ×1

reactjs ×1

redux ×1

redux-saga ×1