如何通过开发者工具以最快、最简单的方式访问API?

问题描述 投票:0回答:1

如何以最快、最简单的方式通过开发者工具访问API?

我找不到一个好的解释让我快速到达这个界面 我找不到一个好的解释让我快速到达这个界面 我想通过开发者管理来做到这一点

json
1个回答
0
投票

不太清楚你的问题是什么。

例如给定一个 API https://httpbin.org/#/Response_formats/get_json 您可以创建一个 JavaScript 函数并将其粘贴到 devtools 控制台

还有 sources 选项卡,您可以在本地计算机上存储 js 文件。

此代码从 httpbin.org 获取数据

function LoadDataFromHttpBin()
{        

    const url = 'https://httpbin.org/json';
    
    // Sending a GET request that only accepts JSON
    fetch(url, {
      method: 'GET', headers: { 'Accept': 'application/json' }
    })
    .then(response => {
      if (!response.ok) {
        // Handle HTTP errors
        throw new Error('Network response was not ok ' + response.statusText);
      }
      // Ensure the response is JSON
      return response.json();
    })
    .then(data => {
      // write the json content to the console
      console.log(data);
    })
    .catch(error => {
      // handle errors 
      console.error('There has been a problem with your fetch operation:', error);
    });
}

您可以将上面的代码粘贴到控制台中,然后只需输入

LoadDataFromHttpBin()

即可调用/使用它

Devtools console

© www.soinside.com 2019 - 2024. All rights reserved.