Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Created February 19, 2019 00:47
Show Gist options
  • Save srkiNZ84/b1d57a7ff9a61408f3125ce5715b7304 to your computer and use it in GitHub Desktop.
Save srkiNZ84/b1d57a7ff9a61408f3125ce5715b7304 to your computer and use it in GitHub Desktop.
Simple example of how to use XHR to call and enpoint
<html>
<head>
<title>Testing XHR</title>
</head>
<body>
<p>This is a test</p>
<br />
<div id="foobar"></div>
<script>
// Create the HTTP request
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if( xhr.status >= 200 && xhr.status < 300){
console.log('success!', xhr);
document.getElementById('foobar').innerHTML = xhr.responseText;
//document.getElementById('foobar').innerHTML = xhr.response;
}
else {
console.log('The request failed!');
}
console.log('End of function');
};
//xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.open('GET', 'https://us-central1-faas-helloworld.cloudfunctions.net/pyhttp');
//xhr.responseType = 'json';
xhr.send();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment