最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use the API key in JavaScript? - Stack Overflow

programmeradmin3浏览0评论

I've been trying to figure out how to use this API, but I'm not sure what it means by needing to "populate the TRN-Api-Key header with your personal API key.". Where would I do this to get results from the API? If I just try putting the API URL with all the things filled out in my browser it doesn't give me a response since I need the API key, but I can't put it in the URL (at least it's not saying anything on how/where to put it). So I was just wondering how to put the API key in so it will let me see the results?

Here's the API I'm trying to use:

BTW, I'm trying to do this with HTML and Javascript / jQuery.

I've been trying to figure out how to use this API, but I'm not sure what it means by needing to "populate the TRN-Api-Key header with your personal API key.". Where would I do this to get results from the API? If I just try putting the API URL with all the things filled out in my browser it doesn't give me a response since I need the API key, but I can't put it in the URL (at least it's not saying anything on how/where to put it). So I was just wondering how to put the API key in so it will let me see the results?

Here's the API I'm trying to use: http://docs.trnbattlefield.apiary.io/#

BTW, I'm trying to do this with HTML and Javascript / jQuery.

Share Improve this question edited Jun 27, 2023 at 17:25 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 19, 2016 at 4:35 Connor SConnor S 391 gold badge1 silver badge3 bronze badges 1
  • Look at the headers section in the jQuery ajax documentation to send the TRN-Api-Key header. – Rickkwa Commented Nov 19, 2016 at 5:00
Add a ment  | 

2 Answers 2

Reset to default 1

You can use jQuery's .ajax() instead of say .get(), which is just a thin wrapper around .ajax() anyway. .ajax() gives you an optional headers parameter where you can add custom headers, such as one with your personal API key. Headers are not part of the URL address itself.

http://api.jquery./jQuery.ajax/

You can use the BF1 Tracker code examples on their site, and have many examples in different languages. As you've asked regarding jQuery, I've copied the code directly from trnbattlefield and added the api-key.

var request = new XMLHttpRequest();

request.open('GET', 'https://battlefieldtracker./bf1/api/Stats/BasicStats?
platform=3&personaId=xxxxxxxxxx&game=tunguska');

request.setRequestHeader('TRN-Api-Key', 'xxxxxxxxxxxxxxxx');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
  }
};

request.send();

Note, this is using the 'DetailedStats' api.

发布评论

评论列表(0)

  1. 暂无评论