Skip to content

Instantly share code, notes, and snippets.

@chongiscool
Created April 24, 2018 14:44
Show Gist options
  • Save chongiscool/227bdee50be6712aba6bb6f0a22c6168 to your computer and use it in GitHub Desktop.
Save chongiscool/227bdee50be6712aba6bb6f0a22c6168 to your computer and use it in GitHub Desktop.
JS 'POST' request template from codecademy.
const xhr = new XMLHttpRequest();
const url = 'https://api-to-call.com/endpoint';
// const url = 'https://www.googleapis.com/urlshortener/v1/url';
const apiKey = 'AIzaSyB_gg7APX8yD_jzBHmTGxxDkhva95Zw808';
const data = JSON.stringify({id: '200'});
xhr.responseType = 'json';
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
console.log(xhr.response);
}
}
xhr.open('POST', url);
xhr.send(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment