Skip to content

Instantly share code, notes, and snippets.

@bryancodesjs
Created January 21, 2022 17:45
Show Gist options
  • Save bryancodesjs/7ae78d95ad3fde6ea94ba599e6bb37d2 to your computer and use it in GitHub Desktop.
Save bryancodesjs/7ae78d95ad3fde6ea94ba599e6bb37d2 to your computer and use it in GitHub Desktop.
How to make an XML HttpRequest
//For this gist, I'm using a free, public API that shows information about a user's location based on their IP address
//here's where I store the API key I was provided with
const myKey = 'c00508dc4104c70518fcf49e17216676';
//here's a random IP from the internet
const myIp = '191.101.63.2';
function myRequest(){
//initialize the XMLHttpRequest object
const request = new XMLHttpRequest();
//set the parameters to be used in the call
request.open("GET", 'http://api.ipstack.com/' + myIp +'?access_key='+ myKey +'&format=1',true);
//send the request
request.send();
//once the response has been received, do XYZ with the response
request.onload = function(){
//parse the response and write it on the console
console.log(JSON.parse(request.responseText));
}
}
myRequest();
myRequest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment