如何使用fetch限制从JSON文件返回的数据量? [英] How to limit the amount of data returned from a JSON file using fetch?

查看:143
本文介绍了如何使用fetch限制从JSON文件返回的数据量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个fetch语句,它返回19个建筑物名称,但是我只想要10个;以下是我尝试过的操作,但仍然得到19个建筑物名称.

I have this fetch statement that returns 19 building names, but I only want 10; the following is what I attempted, but I still get 19 building names.

fetchBuildings(energyProgramId) {
  fetch(`http://localhost:1001/api/energyprograms/${energyProgramId}/buildings/?results=10`)
    .then(res => res.json())
    .then(json => {
      this.setState({
        isLoaded: true,
        buildings: json,
      })
    });
}

我需要添加一些额外的东西吗?

Is there something extra I need to add?

推荐答案

fetchXHRaxios或其他任何内容都没有不同.实际上,与reactangularvue或其他任何东西都没有区别.

No different from fetch to XHR or axios or anything else. actually, no different from react or angular or vue or anything else.

这是后端开发人员编写的API,它基于REST API,因此,当您将其称为GETPOST以及其他任何名称时,您只需获取后端开发人员设计的JSON. BUT

This is an API that backend developers wrote it and it is based on REST API, so when you call it as GET or POST and anything else you just fetch the JSON that the backend developers designed it. BUT

有一项新技术,名称为 GraphQL .您可以调用API,然后只需获取所需的JSON.另外,它必须在后端中实现,但是有可能.

There is a new technology that name is GraphQL. you can call API and then you just fetch the JSON what you want. Also, it must be implemented in backend but it is possible.

这篇关于如何使用fetch限制从JSON文件返回的数据量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆