我正在尝试通过获取API发送获取请求,以请求斗殴之星API服务器。我已经创建了一个与IP地址关联的API密钥。我已经尝试了一切,但是从服务器收到了403响应。
这是我的代码:
const url = 'https://api.brawlstars.com/v1/players/...';
const token = '...';
const headers = new Headers({
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
});
const options = {
method: 'GET',
headers: headers,
mode: 'cors',
cache: 'default'
};
fetch(url, options)
.then(response => response.json())
.then(console.log)
.catch(console.error);
由于控制台策略,在控制台中显示消息:No 'Access-Control-Allow-Origin' header is present on the requested resource
。
当我测试失眠的请求时,效果很好!
前一段时间,当我为Discord机器人执行Brawlstars命令时,我在Brawlstars API上遇到了问题。我能够使API与以下代码一起使用。
const playerurl = 'https://api.brawlstars.com/v1/players/';
const getJSON = async url => {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: 'Bearer <yourapitoken>',
},
});
if(!response.ok) {throw new Error(response.statusText);}
const data = await response.json();
return data;
}
catch(error) {
return error;
}
};
getJSON(playerurl).then(data => {
console.log(data);
}).catch(error => {
console.error(error);
});
我希望这对您有用!