Currently, I am in the process of creating a website for my video game servers. The admin tool we use outputs the current server status in a .json format as a large text string to this specific URL:
My goal is to retrieve the entire text string from the provided URL and store it in a variable so that I can utilize JSON.parse() with the obtained data. Essentially, I want to know how to convert the content of the webpage into a lengthy string that I can then parse using JavaScript.
I am somewhat unsure about the process, but I am confident that once I have the JSON data stored as a string, I will be able to parse it successfully.
Here is an initial attempt at achieving this, which unfortunately did not work as anticipated:
p id="SERVERS">HELLO</p>
<script type="text/javascript>
var txt = document.getElementsByTagName('http://server.bandwidthbandits.org/api/status')[0].innerHTML;
var obj = JSON.parse(txt);
document.getElementById("SERVERS").innerHTML = obj.name + ", " + obj.map + ", " + obj.gamemode + ", " + obj.players;
//more parsing necessary, but to tired to write full statement right now
</script>
After making some updates, the code below showcases what I attempted. Since I will need to query multiple servers, I endeavored to transform the API fetching task into a function. However, I encountered a challenge in importing the API data into something readable for parsing. While manually inputting the API output into my code as a variable works, attempting to import the API data via JavaScript has proved unsuccessful.