如何在HTTP请求中使用API密钥



我很难确定在尝试从端点调用数据时是否有方法使用API键,如果有,我会把它放在哪里?

var request = new XMLHttpRequest("Ocp-Apim-Subscription-Key: {KEY}")
// Open a new connection, using the GET request on the URL endpoint
request.open('GET', 'https://api.sportsdata.io/v3/mlb/scores/json/AreAnyGamesInProgress')
request.onload = function () {
// Begin accessing JSON data here
}
// Send request
request.send()

大多数时候,您需要将它放在HTTP请求头中。

var request = new XMLHttpRequest();
request.open('GET', 'https://api.sportsdata.io/v3/mlb/scores/json/AreAnyGamesInProgress');
request.setRequestHeader("Ocp-Apim-Subscription-Key", "{KEY}");
request.onload = function () {
//...
};
request.send();

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader

最新更新