首页 > 解决方案 > 使用 Javascript 向 ProBasketballAPI.com 发出 POST 请求

问题描述

我正在尝试从 ProBasketballAPI.com API 获取数据。文档说明以下内容:

'要调用 ProBasketball API,您必须向指定的 URL 发出 POST 请求。在这个请求中,您必须传递您的 API 密钥,就是这样。您现在可以访问 API!

当然,您可以传递参数,而不仅仅是您的 API 密钥,事实上,鼓励您这样做。这将有助于缩小将返回的数据范围。

所有响应都将采用 JSON 格式。

我试图将 api_key 作为参数传递,因为它是这样声明的。

const options = {
    'api_key': JSON.stringify(api_key),
    method: 'post',
    headers: {
        'Content-Type': 'application/json'
    }
};

const x = fetch('https://probasketballapi.com/players', options);

x.then((response) => {
    const data = response.json();
    data.then((response) => {
        console.log(response)
    })
})

但我从 api 收到以下错误,

{ errors: [ 'The api key field is required.' ] }

我已经阅读了文档,但我无法弄清楚。该文档可在https://probasketballapi.com/docs/authentication

标签: javascriptpostnetworking

解决方案


推荐阅读