Javascript FORTNITE API请求 [英] Javascript FORTNITE API request
本文介绍了Javascript FORTNITE API请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天我开始使用api学习js,我想问一下如何解决这个问题.
today i started learning js with api and i want to ask how to fix this.
我在Fortnite Tracker上注册并创建了api.
I registred on fortnite tracker and created api.
api就像:
GET https://api.fortnitetracker.com/v1/profile/{platform}/{epic-nickname}
和api键就像:
TRN-Api-Key: xxxxxxx-xxxxxxxxxxx-xxxxxxxxxx
我在这里找到了需要做的事情:
I found something here that i need to do it:
var xhttp = new XMLHttpRequest();
xhttp.request("GET", "api", true);
xhttp.setRequestHeader('TRN-Api-Key' "KEY");
xhttp.send();
但是当我这样做时,它将向我显示错误CORS,我不知道该如何解决.
but when i do it like this, it will show me error CORS and i don't know how ti fix that.
感谢< 3
推荐答案
我没有找到该api的解决方案,所以我在寻找其他api.
I didn't found solution for this api so i looked for other apis.
名称api是:
https://fortnite-public-api.theapinetwork.com/prod09/users/id?username=YOUR_NAME
示例名称api:
https://fortnite-public-api.theapinetwork.com/prod09/users/id?username=Ninja
您获得了UID.您复制该UID并使用platfrom(pc,ps4)将其放置到此api中
there you get UID. You copy that UID and place it to this api with platfrom (pc,ps4)
https://fortnite-public-api.theapinetwork.com/prod09/users/public/br_stats?user_id=UID&platform=pc
示例统计信息API:
https://fortnite-public-api.theapinetwork.com/prod09/users/public/br_stats?user_id=4735ce9132924caf8a5b17789b40f79c&platform=pc
JS代码:
/* your uid and platform */
let uid = "4735ce9132924caf8a5b17789b40f79c";
let platform = "pc";
/* fortnite api link provided by <fortniteapi.com> */
let statApi = "https://fortnite-public-api.theapinetwork.com/prod09/users/public/br_stats?user_id=" + uid + "&platform=" + platform;
httpRequestAsync(statApi, theResponse);
/* getting response from API and defining variables */
function theResponse(response){
console.log("getting stats of " + uid);
let jsonObject = JSON.parse(response);
//example of getting data from api
console.log("Name: " + jsonObject.username);
console.log("Wins: " + jsonObject.totals.wins);
console.log("Kills: " + jsonObject.totals.kills);
console.log("K/D: " + jsonObject.totals.kd);
console.log("Matches: " + jsonObject.totals.matchesplayed);
console.log("Winrate: " + jsonObject.totals.winrate + "%");
};
/* sending XMLHHttpRequest api */
function httpRequestAsync(api, callback){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
callback(httpRequest.responseText);
}
httpRequest.open("GET", api, true);
httpRequest.send();
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
希望对某人有帮助
这篇关于Javascript FORTNITE API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文