我如何制作一个获取函数以从需要身份验证 headers 的 api 中获取 json 数据?

How can i Make a fetch function to get json data from an api that requires authentication headers?

我正在使用 rapidapi 从足球应用程序中获取一些数据,但可以理解为什么会抛出此错误。

我试过这段代码,但它不起作用,这显示了这种类型的错误

Response { type: "cors", url: "https://soccer-livescore.p.rapidapi.com/v1/global/getleague?league=Indo%20D3", redirected: false, status: 403, ok: false, statusText: "Forbidden", headers: Headers, body: ReadableStream, bodyUsed: false }
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-matches',
  templateUrl: './matches.page.html',
  styleUrls: ['./matches.page.scss'],
})
export class MatchesPage implements OnInit {

  constructor(public http: HttpClient){}

  ngOnInit() {


fetch("https://soccer-livescore.p.rapidapi.com/v1/global/getleague?league=Indo%20D3", {
    "method": "GET",
    "headers": {
        "x-rapidapi-host": "soccer-livescore.p.rapidapi.com",
        "x-rapidapi-key": "not showing my rapid key"
    }
})

.then(response => {
    console.log(response);
})
.catch(err => {
    console.log(err);
});
  }

}

您需要注册 RapidAPI 才能使用来自 RapidAPI Hub 的 API。

然后,要使用 API,您需要订阅特定的计划。大多数 API 在 RapidAPI Hub 上都是免费的。

我相信您正在使用足球比分 API;转到 pricing page 并订阅最适合您的计划。