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);
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly