Created
September 5, 2017 15:21
-
-
Save chrxr/96eef4ee85e3846ef9c0f9e9344b92b7 to your computer and use it in GitHub Desktop.
JS script to send data to Hyper server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function() { | |
let button = document.getElementById("submit-button"); | |
console.log(button); | |
button.addEventListener("click", function(event){ | |
event.preventDefault(); | |
send_data("http://localhost:3000") | |
}, false); | |
function send_data(url) { | |
let username = document.getElementById("username").value; | |
let password = document.getElementById("password").value; | |
let json = JSON.stringify({"username": username, "password": password}); | |
let sendData = new Promise((resolve, reject) => { | |
const xhr = new XMLHttpRequest(); | |
xhr.open("POST", url); | |
xhr.setRequestHeader("Content-type", "application/json;charset=utf-8"); | |
xhr.onload = () => resolve(xhr.responseText); | |
xhr.onerror = () => reject(xhr.statusText); | |
xhr.send(json); | |
}); | |
sendData.then((successMessage) => { | |
console.log(successMessage); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment