API and AJAX Flashcards
1
Q
API and AJAX
how to get deta from an XMHL request
A
const req = new XMLHttpRequest(); req.open("GET",'/json/cats.json',true); req.send(); req.onload = function(){ const json = JSON.parse(req.responseText); document.getElementsByClassName('message')[0].innerHTML = JSON.stringify(json); };
2
Q
Using fetch to get data from an API
A
fetch('/json/cats.json') .then(response => response.json()) .then(data => { document.getElementById('message').innerHTML = JSON.stringify(data) })
3
Q
Get Geolocation Data to Find A User’s GPS Coordinates
A
// Add your code below this line if (navigator.geolocation){ navigator.geolocation.getCurrentPosition(function(position) { document.getElementById('data').innerHTML="latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude; }); }
// Add your code above this line
<h4>You are here:</h4>
<div>
</div>