I'm trying to load a very simple text file (eg: 0 online:
or 1 online: Username
or 2 online: Username, Username
), from a server I control, that changes based on the number of users on a minecraft server, into a javascript var, so that I can eventually load it onto my Pebble and have it more or less live update. (read: no jquery) (also read: I have little to no idea what I'm actually doing, and if there's a better way to do it, lemme know.).
I've done a lot of googling which mostly points me to using JSON and XMLHTTPRequests (edit: XMLHTTPRequests are probably necessary) but they seem overly complex for what I need (which is to take the contents of the text file served up via HTTP, stuff it in a var, and have the Pebble spit it out on the screen.)
How can I, without JQuery, and maybe even without JSON, load the contents of a text file into a javascript var?
Some notes:
- The server is eventually going to be accessed via Hamachi, but if I can get it working even locally I'll be pretty thrilled.
- Following this answer, I get this result. Ditto for local IP.
This is very easy to do, just include this wrapper for the
XmlHttpRequest
at the beginning of your script (it makes it easier to use):And then call it like this:
If you format your content in JSON it will actually make things easier for you because you will not have to parse the file and you can use JavaScript to parse it for you automatically:
For example if the reply is:
Then you can process it like this:
For a complete example and the C side (on Pebble), you should take a loot at Step 3 of the Pebble Tutorial, it does exactly this with weather data.