Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Axios如何从api检索最后3篇文章_Reactjs_Axios - Fatal编程技术网

Reactjs Axios如何从api检索最后3篇文章

Reactjs Axios如何从api检索最后3篇文章,reactjs,axios,Reactjs,Axios,我想发出一个get请求,从API返回我的文章数组中的前3篇文章。我到处找,找不到任何能解决我问题的东西 基本上,我想要这样的东西: async componentDidMount() { const { data } = await axios.get(`http://localhost:8000/api/articles/${previous_three_articles}/`) } 我使用了限制偏移分页。>>> 首先,我在my views.py中指定了分页类: class Arti

我想发出一个get请求,从API返回我的文章数组中的前3篇文章。我到处找,找不到任何能解决我问题的东西

基本上,我想要这样的东西:

async componentDidMount() {
    const { data } = await axios.get(`http://localhost:8000/api/articles/${previous_three_articles}/`)
}

我使用了限制偏移分页。>>>

首先,我在my views.py中指定了分页类:

class Articles(viewsets.ModelViewSet):
    # ...
    pagination_class = pagination.LimitOffsetPagination
    queryset = Article.objects.all().order_by('-creation_date')
然后在react中指定了限制:

async componentDidMount() {
    const { data } = await axios.get("http://localhost:8000/api/articles/?limit=3")
    this.setState({
        articles: data.results,
    })
}

你需要告诉我们更多关于API的信息。这完全取决于API的结构,以及如何请求多篇文章。如果不知道您请求的API的详细信息,这是不可能回答的。最简单的方法是获取整个文章列表,然后在客户端手动选择“前三名”。也许你想在API端处理这个问题?如果您可以从客户端传入
page
size
等参数,那么您的API就可以完成其余的工作。我将django与django rest frameworkOk一起使用,但这一切都取决于端点的实现方式。或者,就像@jared所说的,从客户端获取所有文章,对它们进行排序,然后获取前三名。