Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
限制javascript获取的获取结果_Javascript - Fatal编程技术网

限制javascript获取的获取结果

限制javascript获取的获取结果,javascript,Javascript,是否有类似于q=sort&或q=created:&的函数来限制JavaScript获取的结果数量 fetch('https://jsonplaceholder.typicode.com/posts') .then((res) => res.json()) .then((data) => { } 当然,最好的解决方案是如果https://jsonplaceholder.typicode.com/postsendpoint记录了一个可以发送的限制或筛选参数 假设结果是一个数组或

是否有类似于
q=sort&
q=created:&
的函数来限制JavaScript获取的结果数量

fetch('https://jsonplaceholder.typicode.com/posts')
  .then((res) => res.json())
  .then((data) => { }

当然,最好的解决方案是如果
https://jsonplaceholder.typicode.com/posts
endpoint记录了一个可以发送的限制或筛选参数

假设结果是一个数组或包含一个数组,第二个最佳解决方案是结果(应用标准)和/或结果(仅应用限制):

发件人:

不确定您到底想要什么,因此这里有3种可能性:

  • 您可以将有效负载添加到提取的主体中,请参见上文

  • 您可以简单地对其进行url编码

  • 然后((数据)=>{}…您可以筛选所需的数据


  • 希望这能有所帮助。

    关于
    .filter
    ?这不值得投反对票,不值得提出好的问题,也不值得对它进行好的描述,我也在寻找同样的东西
    fetch('https://jsonplaceholder.typicode.com/posts')
        .then((res) => res.json())
        .then((data) => {
            data = data.filter(entry => entry.created > someValue) // Created after X
                       .slice(0, 1000);                            // Limit to 1000
            // ...use data...
        })
        .catch(error => {        // <=== Don't forget to handle errors
            // Handle error...
        });
    
    fetch('https://jsonplaceholder.typicode.com/posts')
        .then((res) => {                                      // ***
            if (!res.ok) {                                    // ***
                throw new Error("HTTP error " + res.status);  // ***
            }                                                 // ***
        })                                                    // ***
        .then((res) => res.json())
        .then((data) => {
            data = data.filter(entry => entry.created > someValue)
                       .slice(0, 1000);
            // ...use data...
        })
        .catch(error => {
            // Handle error...
        });
    
    postData(`http://example.com/answer`, {answer: 42})
      .then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
      .catch(error => console.error(error));
    
    function postData(url = ``, data = {}) {
      // Default options are marked with *
        return fetch(url, {
            method: "POST", // *GET, POST, PUT, DELETE, etc.
            mode: "cors", // no-cors, cors, *same-origin
            cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
            credentials: "same-origin", // include, same-origin, *omit
            headers: {
                "Content-Type": "application/json; charset=utf-8",
                // "Content-Type": "application/x-www-form-urlencoded",
            },
            redirect: "follow", // manual, *follow, error
            referrer: "no-referrer", // no-referrer, *client
            body: JSON.stringify(data), // body data type must match "Content-Type" header
        })
        .then(response => response.json()); // parses response to JSON
    }