0

This is the code I am using to do so.

This code, appears in the documentation, but I'm probably using it wrong, since I'm getting no output.

<script type="text/javascript" src="/js/script.js"></script>
    var xhttp = new XMLHttpRequest();

    xhttp.open("GET", 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/{name}?api_key={key}',true);
    xhttp.send();

    document.write(xhttp.responseText);
</script>


My aim is to get JSON code in respond from the API, and be able to operate with it.
In this case I'm trying to show the code obtained from the api and show it in the page.

Thanks in advance.

sebastomass
  • 1
  • 1
  • 3
  • Looks like you're supposed to fill the `{name}` and `{key}` with your own values. – Clint May 22 '16 at 00:06
  • I changed my name and key to {name} and {key} so it won't show my personal data. – sebastomass May 23 '16 at 14:17
  • That makes sense. I think your `.write` is being called before you actually receive a response. Look into `XMLHttpRequest.onreadystatechange` – Clint May 23 '16 at 14:39

1 Answers1

2
  1. It seems like you are referencing an external JS file called script.js and also trying to use inline Javascript after that but you do not open another script tag for the latter, you only close a non-existent script tag. Try to put your code in between <script> /*code here*/ </script> or in that JS file you reference.

  2. There are libraries and frameworks out there, too numerous to list, but if you must use XMLHttpRequest, take a look at this first.

  3. Some Riot Games API endpoints do not allow CORS, meaning you cannot send these requests from client-side code, you need to send them from a server. source

  4. Check out the interactive Riot Games API reference, you can login and use your API key automatically. You are trying to use Summoner v1.4, however, do consider using Summoner v3 as the former will be deprecated (v3 may not have been existing when you posted the question). Basically, just enter a summoner name and select a region and click Execute Request, a real request will go out and you'll see what was sent and what came back below. Then, you can use the REQUEST URL from there to make your requests.