I'm having an html file location.html and a file with data location.data both on the same webserver in the same directory. the contens of location.data is always the current location data in the form
{"lat":"44.17027","lon":"15.595542","timestamp":"1723663819127","hdop":"2.394","altitude":"510.35","speed":"0.73396146"}
it is written via json_encode() in another php file. Now I need to read this data in a javascript in location.html into an array to display it on a map via leaflet. (It has to be html)
I just can't manage to get this done. I tried XMLHttpRequest() with no success. Would appreciate any other (elegant) method.
<script>
var locx = new XMLHttpRequest();
locx.open('GET', 'location.data');
locx.onreadystatechange = function () {
if (locx.readyState === 4 && locx.status === 200) {
var locationData = locx.responseText;
}
};
locx.send();
</script>
fetchis at least more modern thanXMLHttpRequest, though there's nothing inherently wrong about usingXMLHttpRequest. In what way is the code not working as expected and what debugging have you done? If you're not familiar with your browser's development/debugging tools, now is a great time to start.var locationDataafter it's been set. Could be that you're trying to eat your pizza before it's been delivered. Where exactly is the problem occurring? (eg not making a network request, network request returns wrong data, locationData isn't set).